Note that the error ImportError: cannot import name 'ImageQt' from 'PIL.ImageQt' can be quite misleading.
If you look at the ImageQt.py source code, you'll see that the ImageQt class is only defined if it successfully detects one of the Qt libraries (pyside2/6, PyQt5/6).
So you can get this error if you have a broken install of Qt. You may want to reinstall PySide6 or whatever you've been using.
You could also use a tool like strace and spy on the Python process (if you're using Linux) to see exactly what it's looking for.
strace python -c 'from PIL.ImageQt import ImageQt'
I used this technique to track down why this import was failing under Docker but working properly on a regular desktop.
For the (Ubuntu) Docker container to properly support the import statement, I had to also install these missing libraries:
apt install -y libegl1 libgl1 libxkbcommon0
Answer from Dodgyrabbit on Stack OverflowNote that the error ImportError: cannot import name 'ImageQt' from 'PIL.ImageQt' can be quite misleading.
If you look at the ImageQt.py source code, you'll see that the ImageQt class is only defined if it successfully detects one of the Qt libraries (pyside2/6, PyQt5/6).
So you can get this error if you have a broken install of Qt. You may want to reinstall PySide6 or whatever you've been using.
You could also use a tool like strace and spy on the Python process (if you're using Linux) to see exactly what it's looking for.
strace python -c 'from PIL.ImageQt import ImageQt'
I used this technique to track down why this import was failing under Docker but working properly on a regular desktop.
For the (Ubuntu) Docker container to properly support the import statement, I had to also install these missing libraries:
apt install -y libegl1 libgl1 libxkbcommon0
It looks you are indeed messing with imports. In detail it seems
import PIL.ImageQt as PQ
is in conflict with
self.currentSeg = PQ.ImageQt(self.currentSeg)
If ImageQt is the Object you want to access, then I suggest to change the lines as it follows:
import PIL.ImageQt as PQ
self.currentSeg = PQ(self.currentSeg)
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)
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/
ImageQt does not work as expected in PyQt5
Traceback: ImportError: cannot import name 'ImageQt' from 'PIL'
ImageQt missing import
Import matplotlib qt4 backend after import PIL ImageQt when PyQt5 is installed causes exception
I had the same error. Here was my workflow. I first installed PIL (not Pillow) using
pip install --no-index -f https://dist.plone.org/thirdparty/ -U PIL
Then I found Pillow and installed it using
pip install Pillow
What fixed my issues was uninstalling both and reinstalling Pillow
pip uninstall PIL
pip uninstall Pillow
pip install Pillow
FWIW, the following worked for me when I had this same error:
pip install --upgrade --force-reinstall Pillow
In your PIL image the last band is the alpha channel, whereas in the Qt image the alpha channel is the first (RGBA vs. ARGB). There may be other ways of permuting the bands but the easiest way seems to use the ImageQt class.
from PIL.ImageQt import ImageQt
qim = ImageQt(im)
pix = QtGui.QPixmap.fromImage(qim)
I dont know why, but ImageQt crashed in my system Win10, Python3, Qt5. So i went to an other direction and tried a solution found on github. This code doesnt crash, but gives a effect shown in first post.
My solution for this is, to separate the RGB pic to each color and assemble it as BGR or BGRA before converting it to a Pixmap
def pil2pixmap(self, im):
if im.mode == "RGB":
r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))
elif im.mode == "RGBA":
r, g, b, a = im.split()
im = Image.merge("RGBA", (b, g, r, a))
elif im.mode == "L":
im = im.convert("RGBA")
# Bild in RGBA konvertieren, falls nicht bereits passiert
im2 = im.convert("RGBA")
data = im2.tobytes("raw", "RGBA")
qim = QtGui.QImage(data, im.size[0], im.size[1], QtGui.QImage.Format_ARGB32)
pixmap = QtGui.QPixmap.fromImage(qim)
return pixmap
» pip install pillow