Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"OSError: cannot identify image file" opening image with PIL/Image

I am trying to get some code working that has broken but was working before. I have a PNG file on my desktop and I simply want to open it using the Image module from PIL.

from PIL import Image
img_dir = r'C:\Users\DylanDB\Desktop\square.png'
img = Image.open(img_dir)

This is a remake of my more advanced code that it happens in as well. The error is:

Traceback (most recent call last):
  File "C:/Users/DylanDB/Desktop/img_test.py", line 5, in <module>
    img = Image.open(img_dir)
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 2317, in open
    % (filename if filename else fp))
OSError: cannot identify image file 'C:\\Users\\DylanDB\\Desktop\\square.png'
like image 256
Dylan D.B Avatar asked Sep 09 '16 07:09

Dylan D.B


2 Answers

I had the same error and it was due to the file was recently created and not closed properly before opening with the Image.open(). After closing the file f.close() it werked as expect

like image 105
mahendra Avatar answered Sep 23 '22 01:09

mahendra


I found that the file was a corrupted image.

like image 26
Tom Hale Avatar answered Sep 24 '22 01:09

Tom Hale