Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Python API 2.0 MImage

Anonymous

Python API 2.0 MImage

Anonymous
Not applicable

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.

0 Likes
Reply
1,298 Views
4 Replies
Replies (4)

cheng_xi_li
Autodesk Support
Autodesk Support

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

0 Likes

Anonymous
Not applicable

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

0 Likes

Anonymous
Not applicable

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
0 Likes

Anonymous
Not applicable

Makes sense 🙂 At least it's still maya only and doesn't require something like PIL!

S

0 Likes