AttributeError: module 'PIL.ImageQt' has no attribute 'ImageQt'

3.7k Views Asked by At
import sys
import pickle as p
from PyQt4 import QtCore, QtGui, uic
import numpy as n
import PIL.ImageQt as PQ
from PIL import Image
import cv2
import os

self.currentFrame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
self.currentSeg=Image.fromarray(thresh).convert('RGB')
self.currentSeg = PQ.ImageQt(self.currentSeg)
height,width=self.currentFrame.shape[:2]

im getting above error that imageQt has no attribute. ive tried changing from PIL.ImageQt import ImageQt but it gives ImportError: cannot import name 'ImageQt' from 'PIL.ImageQt'

2

There are 2 best solutions below

0
On BEST ANSWER

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
0
On

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)