Announcements

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

Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Python API 2.0 MImage

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
1296 Views, 4 Replies

Python API 2.0 MImage

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.

4 REPLIES 4
Message 2 of 5
cheng_xi_li
in reply to: Anonymous

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

Message 3 of 5
Anonymous
in reply to: cheng_xi_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

Message 4 of 5
Anonymous
in reply to: Anonymous

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
Message 5 of 5
Anonymous
in reply to: Anonymous

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

S

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report