Created
January 28, 2016 07:12
-
-
Save justinfx/bc9a966507211e98cd37 to your computer and use it in GitHub Desktop.
Passing pixel pointer from a Maya MImage to Qt's QImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Simplified version of original forum post: | |
http://tech-artists.org/forum/showthread.php?4547-Passing-uchar-pointer-with-PySide-in-Maya | |
""" | |
import ctypes | |
import maya.OpenMaya as om | |
from PySide import QtCore, QtGui | |
# Build a test MImage | |
p = "/path/to/test/image.png" | |
mIm = om.MImage() | |
mIm.readFromFile(p) | |
# Prep MImage | |
mIm.verticalFlip() | |
# Get the width and height | |
wUtil = om.MScriptUtil() | |
wUtil.createFromInt(0) | |
wPtr = wUtil.asUintPtr() | |
hUtil = om.MScriptUtil() | |
hUtil.createFromInt(0) | |
hPtr = hUtil.asUintPtr() | |
mIm.getSize(wPtr, hPtr) | |
width = wUtil.getUint(wPtr) | |
height = hUtil.getUint(hPtr) | |
# byte size | |
imSize = width * height * 4 | |
# pass pointer to QImage | |
buf = ctypes.c_ubyte * imSize | |
buf = buf.from_address(long(mIm.pixels())) | |
qIm = QtGui.QImage(buf, width, height, QtGui.QImage.Format_RGB32).rgbSwapped() | |
# Test | |
pix = QtGui.QPixmap.fromImage(qIm) | |
l = QtGui.QLabel() | |
l.setPixmap(pix) | |
l.setScaledContents(True) | |
l.resize(640,480) | |
l.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mottosso I don't even remember saving this! Guess it pays to just document everything incase someone can find it in a search result later.