Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do my Python PIL imports not working?

When I am working with the PIL, I have to import a tons of PIL modules. I was experimenting with three ways to do this, but only the last one works despite all is being logical to me:

Importing the complete PIL and calling it's modules in the code: NOPE

>>> import PIL
>>> image = PIL.Image.new('1', (100,100), 0) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Image'

Importing everything from PIL: NOPE

>>> from PIL import *
>>> image = Image.new('1', (100,100), 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Image' is not defined 

importing some modules from PIL: OK

>>> from PIL import Image
>>> image = Image.new('1', (100,100), 0)
>>> image
<PIL.Image.Image image mode=1 size=100x100 at 0xB6C10F30>
>>> # works...

What did I not get here?

like image 704
Neinstein Avatar asked Jun 12 '16 06:06

Neinstein


People also ask

Why PIL is not working in Python?

The Python "ModuleNotFoundError: No module named 'PIL'" occurs when we forget to install the Pillow module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install Pillow command.

How do I import an image into Python PIL?

To load the image, we simply import the image module from the pillow and call the Image. open(), passing the image filename. Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL).

How do I know if python PIL is installed?

Step 2: To check if PIL is successfully installed, open up the python terminal by typing python3 in the terminal. This will open up the python3 interactive console now type the following command to check the current version of the PIL. This will output the currently installed version of the PIL.

What can I use instead of PIL in Python?

Pillow was announced as a replacement for PIL for future usage. Pillow supports a large number of image file formats including BMP, PNG, JPEG, and TIFF.


1 Answers

PIL doesn't import any submodules on it's own. This is actually pretty common.

So when you use from PIL import Image, you actually locate the Image.py file and import that, whereas when you attempt to just call PIL.Image after import PIL, you're attempting an attribute lookup on an empty module (since you didn't import any submodules).

The same reasoning applies for why from PIL import * won't work - you need to explicitly import the Image submodule. In any case, from ... import * is seen as bad practice due to the namespace pollution that will occur - your best bet is to use from PIL import Image.

Further, PIL is no longer being maintained, but for backwards compatibility purposes should you use from PIL import Image you can ensure your code will remain compatible with the still-maintained Pillow (as oppposed to just using import Image).

like image 183
miradulo Avatar answered Sep 21 '22 23:09

miradulo