In the API 2.0 Mimage.pixels() returns a long pointer to the pixel data. How do I get to the actual pixel data now that 2.0 does not use MScriptUtil? I am brand new to using the API and its a bit confusing.
Hi leo,
You can use uuid.ctypes.cast to wrap it as a c_char and then use uuid.ctypes.string_at to create a cstring for it. The size of the string should be width * height * depth(4).
...
import uuid
...
longPointer = image.pixels() pixel = uuid.ctypes.cast(longPointer, uuid.ctypes.POINTER(uuid.ctypes.c_char)) pixels_size = view.portWidth() * view.portHeight() * 4 # current depth in the document. pixels_string = uuid.ctypes.string_at(pixel, pixels_size)
For more details, please look at the ctypes section in the Python official document.
Yours,
Li
I had the same problem - thanks for the answer!
How would I go about actually getting to the values of those pixels?
I understand that one can create a QImage from the pixels_string - but can I turn that directly into a python array as well?
Thanks!
seb
I ended up just doing it with only Qt like this
def image_to_array(image, width, height): ''' return array of integer 0-255 rgba values [(r, g, b, a)] ''' img = QtGui.QImage(width, height, QtGui.QImage.Format.Format_ARGB32) img.load(image) colorArray = [] for y in range(height): for x in range(width): color = QtGui.QColor() color.setRgba(img.pixel(x,y)) colorArray.append(color.getRgb()) return colorArray
Can't find what you're looking for? Ask the community or share your knowledge.