4

I would like to use ImageQT so I can do image manipulation with the Python Image Library (PIL) and render the result using Qt4. I have a short test routine that reads the using PIL.Image.open, converts it using ImageQT and opens a dialog using QT. If I just use Qt to read the image, it works. What am I missing?

#!/usr/bin/python3.3
import sys
from PIL import Image
from PIL.ImageQt import ImageQt
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QImage
app = QtGui.QApplication(sys.argv)
# added initialization after first suggestion below
QtGui.QImageReader.supportedImageFormats()
im = Image.open('test.gif')
image = ImageQt(im)
pixmap = QtGui.QPixmap(image)
# pixmap = QtGui.QPixmap('test.gif')
widget = QtGui.QWidget()
hbox = QtGui.QHBoxLayout(widget)
lbl = QtGui.QLabel(widget)
lbl.setPixmap(pixmap)
hbox.addWidget(lbl)
widget.setLayout(hbox)
widget.show() 
sys.exit(app.exec_())

test.gif
(source: sjs at www.sonic.net)

Note: added additional QtGui initialization after first suggestion. result is still same.

ekhumoro
122k23 gold badges272 silver badges400 bronze badges
asked Sep 7, 2013 at 16:43

3 Answers 3

7

The problem with the example is that it is attempting to convert a QImage to a QPixmap by passing it directly to the QPixmap constructor, which isn't supported.

Instead, you need to do this:

im = Image.open('test.gif')
image = ImageQt(im)
pixmap = QtGui.QPixmap.fromImage(image)
answered Nov 27, 2013 at 18:59
Sign up to request clarification or add additional context in comments.

Comments

1

To make it work on win + unix the following is sugested:

PilImage = Image.open('kitten.jpg')
QtImage1 = ImageQt.ImageQt(PilImage)
QtImage2 = QtGui.QImage(QtImage1)
pixmap = QtGui.QPixmap.fromImage(QtImage2)
label = QtGui.QLabel('', self)
label.setPixmap(pixmap)

Source: http://skilldrick.co.uk/2010/03/pyqt-pil-and-windows/

answered Sep 16, 2016 at 19:16

1 Comment

AttributeError: type object 'ImageQt' has no attribute 'ImageQt'
0

I don't see you initializing an QtGui.QApplication, this is needed to load the extra formats that Qt supports. I'm not sure if this is neccessary considering you use PIL. But you could try it. See the bottom answer in this post: Possible Solution

answered Sep 14, 2013 at 19:05

2 Comments

Thank you John. The result is the same after addingI initialization code suggested in 'possible solution' link you gave. Result is the same but I need to look at this some more. I added
Thank you @John. The result is the same after adding initialization code suggested in 'possible solution' link you gave. Result is the same but I need to look at this some more. I added a print of the image and get an error saying "OverflowError: can't convert negative value to unsigned int". I also can't print its type or str().

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.