PIL's __init__.py is just an empty stub as is common. It won't magically import anything by itself.
When you do from PIL import Image it looks in the PIL package and finds the file Image.py and imports that. When you do PIL.Image you are actually doing an attribute lookup on the PIL module (which is just an empty stub unless you explicitly import stuff).
In fact, importing a module usually doesn't import submodules. os.path is a famous exception, since the os module is magic.
More info:
The Image Module
PIL's __init__.py is just an empty stub as is common. It won't magically import anything by itself.
When you do from PIL import Image it looks in the PIL package and finds the file Image.py and imports that. When you do PIL.Image you are actually doing an attribute lookup on the PIL module (which is just an empty stub unless you explicitly import stuff).
In fact, importing a module usually doesn't import submodules. os.path is a famous exception, since the os module is magic.
More info:
The Image Module
If you, like me, found the accepted answer a bit befuddling because you could swear you've been able to use
import PIL
PIL.Image
sometimes before, a potential reason for this is if any other code in your Python session has run from PIL import Image or import PIL.Image, even if it's in a completely different scope, you will be able to access PIL.Image.
In particular, matplotlib does so when it's imported. So if you run
import matplotlib
import PIL
PIL.Image
it works. Thanks, Python.
Don't trust anyone. Don't trust Python. Use import PIL.Image.
I am getting AttributeError: module 'PIL' has no attribute 'Image' - Beginner (2018) - fast.ai Course Forums
PIL has no attribute: Image
AttributeError: module 'PIL.Image' has no attribute 'mage'
why not from PIL import Image
Videos
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.
I'm really confused here. I have a_file.py which contains:
from PIL import ImageGrab
image = ImageGrab.grab()
image.save('image.jpg')
and it works fine. Now I want to use ImageGrab in another .py file. I create a new.py file and copy paste the same code.
from PIL import ImageGrab
image = ImageGrab.grab()
image.save('sc.jpg')
Suddenly I get this error:
Traceback (most recent call last):
File "c:/Users/Me/Desktop/new.py", line 1, in <module>
from PIL import ImageGrab
File "C:\python\lib\site-packages\PIL\ImageGrab.py", line 23, in <module>
from . import Image
File "C:\python\lib\site-packages\PIL\Image.py", line 48, in <module>
logger = logging.getLogger(name)
AttributeError: module 'logging' has no attribute 'getLogger'
It doesn't matter where I'm trying to import ImageGrab from PIL, it gives me that error in every single file, except the one file that already had it imported and was working before.
How can I fix that?
» pip install pillow
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)