[PyQt] Odd QImage behavior

David Boddie david at boddie.org.uk
Thu Jan 29 00:39:23 GMT 2009


On Wed Jan 28 11:55:48 GMT 2009, Matt Smith wrote:

> I am constructing QImages from numpy arrays and I use the lines:
>
> nimage = QtGui.QImage(total.data,w,h,QtGui.QImage.Format_Indexed8)
> nimage.ndarray = total
> nimage.setColorTable(COLORTABLE)
>
> If the shape of total is (h,w) this occasionally works. This will fail
> if the shape of the image is an odd size like say (483,621).  It fails
> by wrapping the image in a strange manner.  When I make total a
> corresponding aRGB style array, (483,621,4) it works fine.  (And I don't
> need the colortable).

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.

> Anyway it is working at the moment, I could repeat the problem if it is
> something that needs to be fixed, or looked into.

It's been a while since I've looked at importing data into QImage from
other sources, and I suspect that my last experience was with the Python
Imaging Library which you would expect to output data that QImage is happy
with.

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.

Anyway, those are my guesses.

David


More information about the PyQt mailing list