Pillow not loading image does not identify image file

What happened to the following snippet?

This is not related to the image format, I tried with both jpg and png.

import Image from cStringIO import StringIO with open('/path/to/file/image.png') as f: data = f.read() img = Image.open(StringIO(data)) img.load() Traceback (most recent call last): File "<stdin>", line 4, in <module> File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 2030, in open raise IOError("cannot identify image file") IOError: cannot identify image file 

EDIT:

This happens with a randomly downloaded image from the Internet and the following most basic fragment:

 import Image im = Image.open('WicZW.jpg') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 2030, in open raise IOError("cannot identify image file") IOError: cannot identify image file 
+4
source share
2 answers

The problem was the mutual presence of the PIL library and Pillow on the machine:

 # pip freeze | grep -E '(Pillow|PIL)' PIL==1.1.7 Pillow==2.1.0 
+3
source

I solved it using

 from PIL import Image 

instead of just doing

 import Image 
+3
source

Source: https://habr.com/ru/post/1502827/


All Articles