[PyQt] Odd QImage behavior

David Boddie dboddie at trolltech.com
Thu Jan 29 14:54:10 GMT 2009

On Thu Jan 29 13:41:59 GMT 2009, Matt Smith wrote:

> I made a mistake about what works and what doesn't.  If I use
> nimage = QtGui.QImage(total.data,w,h,QtGui.QImage.Format_Indexed8)
> nimage.setColorTable(COLORTABLE)
> Then there is strange wrapping that occurs if the image is not the correct
> size If I use:
> nimage = QtGui.QImage(total.data,w,h,QtGui.QImage.Format_RGB32)
> nimage.ndarray = total
> Then it works fine, of course for both of those examples 'total' has a
> different shape.

What happens if you reshape the array so that it is a simple one-dimensional
array, or if you pass total.tostring() instead of total to QImage?

> >Maybe there's an issue with padding/alignment in the data - I'm
> >guessing that your numpy array has a compatible type. If your QImage is
> >created using one of the C++ constructors that accepts char* or uchar*
> >I can well imagine that this could be a problem.
> I think that is the problem for two reasons.  First it depends on the
> size of the array, and Second the .ndarray part is left off of first
> example, and doesn't change anything when I include it.

You're just setting an attribute on the object. Was there a guide somewhere
that said you have to do that?

> > Having said that, you would have thought that it wouldn't have a problem
> > with raw bytes. Maybe look at the size of the data obtained from the
> > array and see if it equals what you would expect.
> I have checked the data by comparing sizes.  Also I implement a line
> detection algorithm and the lines are drawn according to the correct
> shape, while the data is displayed warped.

Strange. I'm assuming that the data had the expected size. Without access to
the data, it's difficult to track down the bug, though. I guess you or I
could create a simple test case with a numpy array. Do you want to give it
a try?


More information about the PyQt mailing list