Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL Image ImportError

I have Pillow and qrcode modules installed in a virtual environment.

From the python shell, I can create a test image programmatically using PIL:

>>> from PIL import Image
>>> img = Image.new('1', (200, 200))
>>> img.save('test-image.jpeg', 'JPEG')

Great, that works just as I would expect it to. However, I'm getting this error when I try to use a module that relies on PIL:

>>> import qrcode
>>> qr_code = qrcode.make("1") 
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/home/vagrant/.virtualenvs/env1/local/lib/python2.7/site-packages/qrcode/main.py", line 8, in make
     return qr.make_image()
   File "/home/vagrant/.virtualenvs/env1/local/lib/python2.7/site-packages/qrcode/main.py", line 186, in make_image
     from qrcode.image.pil import PilImage
   File "/home/vagrant/.virtualenvs/env1/local/lib/python2.7/site-packages/qrcode/image/pil.py", line 5, in <module>
     import Image
ImportError: No module named Image

Why can't qrcode import PIL's Image class but it works from the shell?

like image 948
Marc Wilson Avatar asked Oct 01 '13 21:10

Marc Wilson


People also ask

What is PIL image image?

Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.

How do I view an image using PIL?

Image. open() Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method).

How do I fix No module named PIL 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.


1 Answers

This is an issue with your installation: Image module have been installed as subpackage of a PIL module, while the library you are using expects Image module to be directly in the python path. Simplest solution is to replace:

import Image

with:

from PIL import Image

in file qrcode/image/pil.py.

like image 109
Stefano Sanfilippo Avatar answered Sep 27 '22 18:09

Stefano Sanfilippo