Image of current view to base64

Hi there!
I'm trying to convert the image/screenshot of the current view to a base64 string.
In c++ I would have done something like this:

    QByteArray byteArray;
    QBuffer buf(&byteArray);
    QImage viewPortImage(100,100,QImage::Format_ARGB32);, "png");
    std::string encoded = byteArray.toBase64().toStdString();

I'm not able to translate this for the usage with the Python interpreter and pya, since QByteArray is not wrapped (and possibly other issues).

Trying to get the raw data with the following code fails:

      view = pya.Application.instance().main_window().current_view()
      viewPortImage = view.get_image(100,100)

with: "'utf-8' codec can't decode byte 0xff in position 0: invalid start byte in QImage_Native.bits"
The wrapper documentation tells me that the data is converted to a python string, which apparently fails.

Signature: [const] string bits
Description: Method const unsigned char *QImage::bits()

Right now I'm using view.save_image(imagePath,100,100) to save the image to disk where I can open the file and encode it with python's base64, but I'm wondering if there's a better solution.


  • Hi,

    you're right, the problem is to translate the data into a byte-string in Python. The Qt binding is somewhat aged and was created when there wasn't bytes() and strings have not been so picky about encoding.

    Right now, the file save/read way is the best workaround. I have created a ticket


Sign In or Register to comment.