[Image-SIG] A possible bug in converting palletized images to QImage

Mauricio Muñoz Lucero real.mml at gmail.com
Sun Dec 27 21:09:42 CET 2009


I am making an image converter using PIL 1.1.7 and PyQt 4.6 for the gui.

Everything goes perfect, except when i try to convert an image in "P"
mode (8 bit pixels and using a colour palette) to QImage, using the
ImageQt module.

self.img = ImageQt.ImageQt(self.raw_image)

In this case, always i get a white image, it doesn't appear any error messages.

But if i copy the code of the conversion from the ImageQt module, i
get the next error:

  File "cpcview.py", line 482, in initialize_image_to_convert
    palette_image = self.raw_image.getpalette()
  File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 932, in
  File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 127,
in load
    pixel = Image.Image.load(self)
  File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 608, in
    if self.im and self.palette and self.palette.dirty:
OverflowError: can't convert negative value to unsigned long

The possible bug appears in the getpalette() function, i am using now
a workaround to fix it, using "palette.getdata()" instead of
"getpalette()". This is the workaround in my program:

self.img = ImageQt.ImageQt(self.raw_image)

# Workaround to "OverflowError: can't convert negative value to unsigned long"
if self.imagen_cruda.mode == "P":
    palette_image = self.imagen_cruda.palette.getdata()
    palette_image = palette_image[1]
    colorlist = []
    for i in range(len(palette_image) // 3):
          colorlist.append(QtGui.qRgba(ord(palette_image[i * 3]),
                                  ord(palette_image[i * 3 + 1]),
                                  ord(palette_image[i * 3 + 2]), 0xFF))

Is it really a bug or am i using incorrectly the ImageQt module?
Is there a better or more "pythonic" fix to this problem?

Thanks for this wonderful library,

More information about the Image-SIG mailing list