image lib & Qt4

A

aljosa

i'm looking for image lib which supports common image types (maybe
freeimagepy?) and is relatively easy to display image loaded through
that lib in PyQt4.
any ideas?

Aljosa
 
D

Diez B. Roggisch

aljosa said:
i'm looking for image lib which supports common image types (maybe
freeimagepy?) and is relatively easy to display image loaded through
that lib in PyQt4.
any ideas?

Use PIL & StringIO to create a in-memory representation of the image as e.g.
PNG. Load that using Qt.

Diez
 
A

aljosa

does anybody know how to load <StringIO.StringIO instance> (in-memory
representation of PNG image) from Qt (v.4.1.3) as QImage?
 
F

Fredrik Lundh

aljosa said:
does anybody know how to load <StringIO.StringIO instance> (in-memory
representation of PNG image) from Qt (v.4.1.3) as QImage?

If you want the data from a StringIO object, you can either *read* the
data from the object (it's a file-like object, after all), or use the
getvalue() method.

Something like

qim = QImage()
qim.loadFromData(f.getvalue())

should work (unless they've changed things around lately).

To get better performance, you should be able to use PIL's tostring()
method together with the QImage(buffer, width, height, depth,
colortable, numColors, bitOrder) form of the QImage constructor.

</F>
 
F

Fredrik Lundh

Fredrik said:
To get better performance, you should be able to use PIL's tostring()
method together with the QImage(buffer, width, height, depth,
colortable, numColors, bitOrder) form of the QImage constructor.

for PyQt4, that seems to have changed to QImage(buffer, width, height,
format), where format is a QImage.Format_XXX specifier.

in other words, this should work:

if im.mode != "RGB":
im = im.convert("RGB")

data = im.tostring("raw", "BGRX")

image = QImage(data, im.size[0], im.size[1],
QImage.Format_RGB32)

note that the QImage descriptor will point into the data buffer, so you
must hang on to data for as long you're using image.

(I haven't found a way to attach a user attribute to a QImage class; it
doesn't complain when I do that, but it doesn't seem to do the right
thing...)

</F>
 
F

Fredrik Lundh

I've posted a simple ImageQt interface module for PIL and PyQt4 here:

http://svn.effbot.python-hosting.com/stuff/sandbox/pil/ImageQt.py

this interface handles 1, P, L, and RGB images efficiently. usage:

im = Image.open(...) # or some other pil operation

qim = ImageQt.toimage(im)

# to access the QImage, use qim.image. e.g:

label.setPixmap(QtGui.QPixmap.fromImage(qim.image))

note that the toimage() function returns a wrapper object, not a QImage
instance. I didn't find a reliable way to subclass from QImage, nor any
other way to attach custom attributes to an existing QImage object. if
anyone knows how to do that, let me know.

</F>
 
K

K.S.Sreeram

Fredrik said:
Fredrik Lundh wrote:

I've posted a simple ImageQt interface module for PIL and PyQt4 here:

http://svn.effbot.python-hosting.com/stuff/sandbox/pil/ImageQt.py

I'm getting an 'unknown raw mode' error on ImageQt.py:59:
data = im.tostring( "raw", "BGRX" )

Does this only work with PIL 1.1.6 snapshots? I currently have PIL 1.1.5.

Regards
Sreeram


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEgYIHrgn0plK5qqURAhPfAKCbN/QfOvWhy9Wb0YlyP02LOTUorACffI2m
VCsdYHRYg3WVv24Pq42SSyE=
=x92s
-----END PGP SIGNATURE-----
 
F

Fredrik Lundh

K.S.Sreeram said:
I'm getting an 'unknown raw mode' error on ImageQt.py:59:
data = im.tostring( "raw", "BGRX" )

Does this only work with PIL 1.1.6 snapshots? I currently have
> PIL 1.1.5.

it was developed on Python 2.4.3 and PIL 1.1.5, so it should work. and
BGRX support has been in there for ages (at least since 1.1.1).

what happens if you do:

$ python40000

</F>
 
K

K.S.Sreeram

My bad.
I was hacking the code trying to support RGBA mode images, and
inadvertently i tried im.tostring("raw","BGRX") on the RGBA mode image.

So BGRX does indeed work for RGB mode images.

While trying to support RGBA mode images, i realized that a BGRA raw
mode is needed for working with QImage. It doesn't seem to be supported
by PIL. Any workarounds?

Regards
Sreeram

Fredrik said:
$ python
40000


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEgZGrrgn0plK5qqURAsi3AKCUTxmAs2Bqf0RHhoRN3xmX6fUDYwCfYipT
ub7nCNwevuTdNNt9yZBRa5w=
=9oVP
-----END PGP SIGNATURE-----
 
F

Fredrik Lundh

K.S.Sreeram said:
I was hacking the code trying to support RGBA mode images, and
inadvertently i tried im.tostring("raw","BGRX") on the RGBA mode image.

So BGRX does indeed work for RGB mode images.

While trying to support RGBA mode images, i realized that a BGRA raw
mode is needed for working with QImage. It doesn't seem to be supported
by PIL. Any workarounds?

sure! here's a patch:

Index: ImageQt.py
===================================================================
--- ImageQt.py (revision 342)
+++ ImageQt.py (working copy)
@@ -58,6 +61,14 @@
elif im.mode == "RGB":
data = im.tostring("raw", "BGRX")
format = QImage.Format_RGB32
+ elif im.mode == "RGBA":
+ try:
+ data = im.tostring("raw", "BGRA")
+ except SystemError:
+ # workaround for earlier versions
+ r, g, b, a = im.split()
+ im = Image.merge("RGBA", (b, g, r, a))
+ format = QImage.Format_ARGB32
else:
raise ValueError("unsupported image mode %r" % im.mode)

</F>
 
K

K.S.Sreeram

thanks!
so does that mean the BGRA raw mode is supported in PIL 1.1.6?

Regards
Sreeram

Fredrik said:
sure! here's a patch:

Index: ImageQt.py
===================================================================
--- ImageQt.py (revision 342)
+++ ImageQt.py (working copy)
@@ -58,6 +61,14 @@
elif im.mode == "RGB":
data = im.tostring("raw", "BGRX")
format = QImage.Format_RGB32
+ elif im.mode == "RGBA":
+ try:
+ data = im.tostring("raw", "BGRA")
+ except SystemError:
+ # workaround for earlier versions
+ r, g, b, a = im.split()
+ im = Image.merge("RGBA", (b, g, r, a))
+ format = QImage.Format_ARGB32
else:
raise ValueError("unsupported image mode %r" % im.mode)

</F>



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD4DBQFEgZrzrgn0plK5qqURAgc1AJ9Ld4X73U62qi4HYli8hcLgrrTTjgCY8hVm
PCrSGALbK953GPytBSGDgQ==
=ZqNw
-----END PGP SIGNATURE-----
 
F

Fredrik Lundh

K.S.Sreeram said:
so does that mean the BGRA raw mode is supported in PIL 1.1.6?

possibly; I haven't checked. it does mean that some future version will
most likely support it, at least.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top