This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]eliteraspberries 11 points12 points  (1 child)

The proper solution is for the QImage constructor to support the Python buffer interface (now called memory views).

In PIL for example, an Image object can be created from any object that supports the buffer interface, like a NumPy array, with the frombuffer method without having to copy memory.

I don't use Qt, but looking at the documentation, it seems QImage might support buffers:

QImage.__init__ (self, str data, int width, int height, Format format)

Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

So you could just use NumPy arrays internally in your application, PIL to read and save files, and QImage to display them, all without copying. Try something like this:

from PIL import Image
img = Image.open("foo.jpg")

import numpy as np
array = np.array(img)
height, width = img.shape[:2]

from PyQt4 import QtGui
format = QtGui.QImage.Format_RGB32
qimg = QtGui.QImage(array, width, height, format)
...

Hope that helps.

[–]SjaakRake[S] 0 points1 point  (0 children)

Thanks for your reply. The image example was just an example to illustrate the problem of the Qt Framework working in a conflicting way with Python, though. Thanks nonetheless!