By Sibbs Gambling

2013-10-07 17:24:53 8 Comments

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 = + '\\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\", line 19, in <module>
    im = + '\\ST.jpg')
  File "C:\Python27\lib\site-packages\PIL\", 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!


@Deil 2019-09-24 13:48:42

For anyone who make it in bigger scale, you might have also check how many file descriptors you have. It will throw this error if you ran out at bad moment.

@pinkie pAI 2019-07-07 19:16:58

In my case, it was because the images I used were stored on a Mac, which generates many hidden files like .image_file.png, so they turned out to not even be the actual images I needed and I could safely ignore the warning or delete the hidden files. It was just an oversight in my case.

@jchmyz 2019-05-08 21:13:49

In my case the image file had just been written to and needed to be flushed before opening, like so:

img =

@DavidCH12345 2019-03-19 17:19:26

In my case there was an empty picture in the folder. After deleting the empty .jpg's it worked normally.

@panofish 2019-02-07 23:27:57

In my case.. I already had "from PIL import Image" in my code.

The error occurred for me because the image file was still in use (locked) by a previous operation in my code. I had to add a small delay or attempt to open the file in append mode in a loop, until that did not fail. Once that did not fail, it meant the file was no longer in use and I could continue and let PIL could now open the file.

@Anoop Sharma 2018-12-26 11:59:51

Seems like a Permissions Issue. I was facing the same error. But when I ran it from the root account, it worked. So either give the read permission to the file using chmod (in linux) or run your script after logging in as a root user.

@iamsk 2015-11-13 06:25:40

first, check your pillow version

python -c 'import PIL; print PIL.PILLOW_VERSION'

I use pip install --upgrade pillow upgrade the version from 2.7 to 2.9(or 3.0) fixed this.

@Philip 2019-10-21 13:29:53

I think this helped a lot, but I still get the error FileNotFoundError: [Errno 2] No such file or directory

@ThinkFloyd 2017-12-14 10:57:35

If you are using Anaconda on windows then you can open Anaconda Navigator app and go to Environment section and search for pillow in installed libraries and mark it for upgrade to latest version by right clicking on the checkbox.

Screenshot for reference:enter image description here

This has fixed the following error:

PermissionError: [WinError 5] Access is denied: 'e:\\work\\anaconda\\lib\\site-packages\\pil\\_imaging.cp36-win_amd64.pyd'

@musicakc 2014-03-12 11:49:08

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:


and then open the file

@naoko 2013-12-31 21:02:15

I had a same issue.

from PIL import Image

instead of

import Image

fixed the issue

@Luke Sapan 2014-01-29 18:57:09

Not sure why this was downvoted, but this worked for me too. I almost didn't try it because it was -1.

@Deep-B 2014-03-07 19:53:58

Didn't work for me... though my issue is intermittent, so not sure that qualifies...

@A.J. 2014-03-08 09:55:42

IOError("cannot identify image file") didnt work for me too.

@Evgeni Sergeev 2014-08-06 06:28:42

This worked for me when was suddenly unable to open JPEGs any more. Can anyone explain the reason why this import statement change changes behaviour? I checked, and the only under site-packages is the one in PIL. I thought that from ... import ... should work identically with import ..., other than how it places the resulting object in the global namespace -- in this case, it should be completely identical, no?

@irenemeanspeace 2015-11-13 10:01:12

This worked for me too. But like what Evgeni said, can anyone explain why this works?

@albfan 2016-05-07 13:55:06

On PIL/ImageTk from PIL import Image is used. Using import Image creates two instances of Image module, and collected plugins for treat images Image.ID is duplicated too. One filled with plugins, other empty. Your code uses empty one. More here

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

16 Answered Questions

[SOLVED] What are metaclasses in Python?

63 Answered Questions

[SOLVED] Calling an external command from Python

37 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

36 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3089596 View
  • 2634 Score
  • 36 Answer
  • Tags:   python datetime time

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

29 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3537413 View
  • 2901 Score
  • 29 Answer
  • Tags:   python list indexing

16 Answered Questions

[SOLVED] How do I copy a file in Python?

21 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3770539 View
  • 3474 Score
  • 21 Answer
  • Tags:   python directory

22 Answered Questions

[SOLVED] Creating a singleton in Python

Sponsored Content