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

Sibbs Gambling picture Sibbs Gambling · Oct 7, 2013 · Viewed 125.9k times · Source

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!

Answer

naoko picture naoko · Dec 31, 2013

I had a same issue.

from PIL import Image

instead of

import Image

fixed the issue