[PyKDE] (transparent) PIL image to QImage conversion?

David Boddie david at boddie.org.uk
Sun Apr 23 22:20:53 BST 2006


On Thu, 6 Apr 2006 16:37:07, "ORCAN OGETBIL" wrote:

> My problem is, the JPEG encoding kills the transparency of my PNG images.
> When I do RAW encoding, I get nothing from the QByteArray object. I also
> couldn't get the thing work with a GIF encoding. PIL's tostring() function
> doesn't support PNG encoding.
>
> Here I found a list of encodings PIL provides:
> http://mail.python.org/pipermail/image-sig/2004-September/002908.html
>
> Has anybody encountered this problem?
> Can I use cStringIO.StringIO() instead of PIL's tostring() somehow?
> Any solutions?

If you haven't already solved this problem, you might want to try saving
the contents of the Image to a StringIO object then reading the data
stored in the StringIO into a QImage. Try something like this:

  # im is an existing PIL Image object.
  from qt import QImage
  from cStringIO import StringIO
  s = StringIO()
  im.save(s, "PNG")
  s.seek(0)
  image = QImage()
  image.loadFromData(s.read())

Hope this is useful,

David




More information about the PyQt mailing list