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)
Loading PIL Image to QPixMap
ImageQt does not work as expected in PyQt5
python - why " attribute error: module PIL has no attribute Image" raises unlike its installed and code is correct - Stack Overflow
PIL has no attribute: Image
Hey, everyone!
I'm trying to execute this notebook, but I came across an attribute error: PIL has no attribute Image.
I tried implementing "from PIL import Image" , and other common fixes available on StackOverflow, but I didn't get anywhere. I want to be able to plot the images. And not using matplotlib. Is there any workaround to this?
This is a known versioning issue between cinnamon and pillow >= 6.0.0. You can find more info here. As a previous commenter said, you can find the error in /usr/share/cinnamon/cinnamon-settings/bin/imtools.py. However, changing Image.VERSION to PIL.VERSION will not fix the issue for pillow >= 7.0.0. You must instead change the line to if Image.__version__ == '1.1.7':.
If you are comfortable with python. You can modify /usr/share/cinnamon/cinnamon-settings/bin/imtools.py.
- Make a backup of the file. i.e
sudo cp /usr/share/cinnamon/cinnamon-settings/bin/imtools.py /usr/share/cinnamon/cinnamon-settings/bin/imtools.py.bk
- Open
/usr/share/cinnamon/cinnamon-settings/bin/imtools.pywith nano
sudo nano /usr/share/cinnamon/cinnamon-settings/bin/imtools.py
- Delete line 623 to 636. And shift lines 637 to 645 4 spaces to the left.
Before:
if Image.VERSION == '1.1.7':
def split(image):
"""Work around for bug in Pil 1.1.7
:param image: input image
:type image: PIL image object
:returns: the different color bands of the image (eg R, G, B)
:rtype: tuple
"""
image.load()
return image.split()
else:
def split(image):
"""Work around for bug in Pil 1.1.7
:param image: input image
:type image: PIL image object
:returns: the different color bands of the image (eg R, G, B)
:rtype: tuple
"""
return image.split()
After:
def split(image):
"""Work around for bug in Pil 1.1.7
:param image: input image
:type image: PIL image object
:returns: the different color bands of the image (eg R, G, B)
:rtype: tuple
"""
return image.split()
A check for version Image.VERSION 1.1.7 is not needed if you are using current version of PIL.
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/
SOLVED: Turns out BYU wrote a library specifically for the course which replaces PIL's Image, despite them giving explicit instructions to use PIL. Disregard if not specifically working on project one of CS111 at BYU. If you happen to be doing so, go look at the slides for lecture 7. You will need to import the byuimage library, which is not included in any of the distribution code, even though PIL imports are.
Hey guys! I'm working on some projects necessary to get a class waived at the school I'll (pending aceptance) be going to next fall. My experience with python is, well, very little, but the intro class uses python so I'm doing the work in Python.
The first project entails a simple "image processing" program. I've got to the first point to run a pytest (I have zero prior experience with pytest, so if I've done something done there, my bad), which is just to open and display the passed image. Program works as intended, but when I run the pytest written by the school, I get the following error.
__test_display_image ______________________________
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x0000024D0468D2D0>
@max_score(20)
def test_display_image(monkeypatch):
observed = None
@functools.wraps(Image.show)
E AttributeError: module 'PIL.Image' has no attribute 'show'
test_project1.py:23: AttributeErrorAt first, I assumed that Pillow wasn't getting imported correctly, but the import line is the same in teh pytest as in the program.
from PIL import Image
After a good deal of googling, I've found similar looking errors dealing with other methods of Image, but none concerning .show, and they all have wildly different reasons, so I'm at a bit of a loss. The pytest and program files are in the same directory. I'm sure it's something dumb, but I'm not really sure what else to look at, so I'd love to have my dumb mistake pointed out! Thank you all!
Pytest code
from PIL import Image
from byu_pytest_utils import max_score, run_python_script, test_files
import functools import pytest
def assert_equal(observed: Image, expected: Image):
assert observed.width == expected.width assert observed.height ==
expected.height for y in range(observed.height):
for x in range(observed.width):
observed_pixel = observed.get_pixel(x, y) expected_pixel =
expected.get_pixel(x, y)
assert observed_pixel.red == expected_pixel.red, f"the pixels at
({x}, {y}) don't match"
assert observed_pixel.green == expected_pixel.green, f"the pixels at
({x}, {y}) don't match"
assert observed_pixel.blue == expected_pixel.blue, f"the pixels at
({x}, {y}) don't match"
@max_score(20) def test_display_image(monkeypatch):
observed = None @functools.wraps(Image.show)
def patched_Image_show(self):
nonlocal observed
observed = self
monkeypatch.setattr(Image, 'show', patched_Image_show)
run_python_script('image_processing.py', '-d',
test_files / 'explosion.input.png')
if observed is None:
pytest.fail('No Image was shown')
assert_equal(observed, Image(test_files / 'explosion.input.png'))Program code (though I don't think it should matter)
import sys
from PIL import Image
def main():
if not validate_command(sys.argv):
return
run_command()
def validate_command(args):
try:
if args[1][0:1] == '-' and args[2] != None:
print(f"Running image processor with aruments {args[1]}, {args[2]}")
return True
else:
print("Invlaid arguments. Usage: image_processing.py <operation>
[<argument 1> <argument 2> ...]")
except: print("Invlaid arguments. Usage: image_processing.py <operation>
[<argument 1> <argument 2> ...]")
return False
def display(image):
image.show()
def run_command():
image = Image.open(sys.argv[2])
commands[sys.argv[1]](image)
commands = {"-d": display}main()
(Pasting into reddit messed up the formatting, so it's possible I made a mistake when correcting the formatting)