Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image.open() cannot identify image file - Python?

I am running Python 2.7 in Visual Studio 2013. The code previously worked ok when in Spyder, but when I run:

import numpy as np import scipy as sp import math as mt import matplotlib.pyplot as plt import Image import random  # (0, 1) is N SCALE = 2.2666 # the scale is chosen to be 1 m = 2.266666666 pixels MIN_LENGTH = 150 # pixels  PROJECT_PATH = 'C:\\cimtrack_v1' im = Image.open(PROJECT_PATH + '\\ST.jpg') 

I end up with the following errors:

Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "C:\cimtrack_v1\PythonApplication1\dr\trajgen.py", line 19, in <module>     im = Image.open(PROJECT_PATH + '\\ST.jpg')   File "C:\Python27\lib\site-packages\PIL\Image.py", line 2020, in open     raise IOError("cannot identify image file") IOError: cannot identify image file 

Why is it so and how may I fix it?


As suggested, I have used the Pillow installer to my Python 2.7. But weirdly, I end up with this:

>>> from PIL import Image Traceback (most recent call last):   File "<stdin>", line 1, in <module> ImportError: No module named PIL   >>> from pil import Image Traceback (most recent call last):   File "<stdin>", line 1, in <module> ImportError: No module named pil  >>> import PIL.Image Traceback (most recent call last):   File "<stdin>", line 1, in <module> ImportError: No module named PIL.Image  >>> import PIL Traceback (most recent call last):   File "<stdin>", line 1, in <module> ImportError: No module named PIL 

All fail!

like image 701
Sibbs Gambling Avatar asked Oct 07 '13 17:10

Sibbs Gambling


People also ask

How do I open an image file in Python?

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 load 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).


2 Answers

I had a same issue.

from PIL import Image 

instead of

import Image 

fixed the issue

like image 93
naoko Avatar answered Sep 17 '22 20:09

naoko


So after struggling with this issue for quite some time, this is what could help you:

from PIL import Image 

instead of

import Image 

Also, if your Image file is not loading and you're getting an error "No file or directory" then you should do this:

path=r'C:\ABC\Users\Pictures\image.jpg' 

and then open the file

image=Image.open(path) 
like image 44
musicakc Avatar answered Sep 18 '22 20:09

musicakc