Pillow: OSError: cannot identify image file png

Created on 5 Aug 2018  Â·  16Comments  Â·  Source: python-pillow/Pillow

What did you do?

Traceback (most recent call last):
  File "main.py", line 806, in main
    img = Image.open(s)
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2622, in open
    % (filename if filename else fp))
OSError: cannot identify image file <_io.BytesIO object at 0x00000222E0B9E468>
png
Couldn't compress content for ('https://cdn.wccftech.com/wp-content/uploads/2017/11/windows-10-iphone-1480x897-86x60.png', b'')

s = io.BytesIO(self.website_cache[entry][0].raw_content)
img = Image.open(s)
s2 = io.BytesIO()

It's random, but sometimes I have images that are not being read correctly and displaying this error. It can be different common file formats such as png, jpg, jpeg, etc and it displays this error. I am running python 3.7 on a Windows 10.

Most helpful comment

image has extra 15 bytes in the beginning and it seems to be truncated. Works that way:

```python
from io import BytesIO
from PIL import Image, ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True

with open('broken.png', 'rb') as f:
b = BytesIO()
f.seek(15, 0)

b.write(f.read())

im = Image.open(b)
im.load()

All 16 comments

Could you post a self-contained code example? For instance, in the code that you have provided so far, I don't have a value for self.website_cache[entry][0].raw_content.

At the moment, the closest way I can replicate your situation is to fetch data from the URL you have provided, put the value into a BytesIO object, and load it into an image.

import io
import urllib.request
from PIL import Image
url = 'https://cdn.wccftech.com/wp-content/uploads/2017/11/windows-10-iphone-1480x897-86x60.png'
with urllib.request.urlopen(url) as f:
    b = io.BytesIO(f.read())
    im = Image.open(b)
    im.save('out.png')

I am able to run this code without any problems. Are you able to do so? If you are, then we would need more code to be able to replicate the issue.

This might be hard to test, but here is one image that fail to load. I save the input in wb mode in python there. Maybe it has to do with the way it is reading the raw content from the web. I did open the file in Gimp and it seem to load just fine.

testfile

Using the following code you provided I was able to still get the same error message:
File "G:\Test\Newfolder\test.py", line 6, in
im = Image.open(b)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2622, in open
% (filename if filename else fp))
OSError: cannot identify image file <_io.BytesIO object at 0x0000022542C641A8>

Here is the mod code I did use:

import io
import urllib.request
from PIL import Image
with open("testfile.png", "rb") as f:
    b = io.BytesIO(f.read())
    im = Image.open(b)
    im.save('out.png')

Thanks. The image you have attached does load in Gimp, but does not load in Preview on macOS, or in Chrome. So it is malformed in some way, and we would need to figure out how to get Pillow to accept that data.

image has extra 15 bytes in the beginning and it seems to be truncated. Works that way:

```python
from io import BytesIO
from PIL import Image, ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True

with open('broken.png', 'rb') as f:
b = BytesIO()
f.seek(15, 0)

b.write(f.read())

im = Image.open(b)
im.load()

Cool, thanks @kkopachev. So @Mradr, do you think that there is a problem in your code that is creating the extra 15 bytes?

@kkopachev, thanks
@radarhere, no not with in my code that is creating the extra bytes that I know of it. It should the right. I mean if gimp can open it - it can't be "extra" otherwise the image wouldn't load, no? That tells me it's a different format type of some sort would it not? I been shaving the images that fail to load as well to check out later. If it was all of them I would agree it's my code, but it's only been a few from a few different sites. I will test them with this code and see what happens. I'm sure it's extra data, but there would have to be a way to read for that in this case instead of "try/excepting" it all the time for the extra data.

https://www.w3.org/TR/PNG-Structure.html - ‘The first eight bytes of a PNG file always contain the following (decimal) values: 137 80 78 71 13 10 26 10’.

Running the following -

with open('43690627-ae3f2330-98d2-11e8-97be-51e90f41fd5d.png', 'rb') as fp:
    data = fp.read()
    out = []
    for char in data[:23]:
        out.append(str(char))
    print(' '.join(out))

gives '31 139 8 0 0 0 0 0 0 3 0 59 64 196 191 137 80 78 71 13 10 26 10'. So your file has extra bytes at the beginning, meaning it does not conform to the specification. Gimp is being flexible in it's interpretation of your file.

@Mradr was this resolved? I'm facing the same issue, with some images I downloaded from the web working and others are giving the same error.

@nadzimo I'd be surprised if part of your problem was also extra bytes at the beginning of images. Try https://github.com/python-pillow/Pillow/issues/1510#issuecomment-151458026 to see if you are dealing with truncated images. If not, then I would recommend opening a new issue.

@Mradr did you ever find a problem in your code? The alternative is that this issue is you making a request for Pillow to search through provided image data until it recognises the start of an image.

Okay, without any more responses, I am closing this issue, as directly accessing the image at the URL works. The only thing Pillow doesn't do in this issue is read a malformed file, which may have been malformed as a result of a bug unrelated to Pillow. Feel free to comment again if you would like to pursue this further.

Hi, I have the same issue but the image that I tried to open is previously resampled from square pixels to hexagonal pixels. Can pillow accept this type?

image = Image.open('anna.jpg').convert('L')
h = hipsample(image,6,1.0,BCUBIC)
h.save('hex1.png')
image2 = Image.open('hex1.png')`

hipsample() function is the conversion from square to hex. So the image cant be open by pillow.
Will this be acceptable using some tweaks in the code?

@FrancoJigo while I'm not aware of anything within Pillow regarding hexagonal pixels, would you be able to open a new issue with your problem, to help avoid confusion?

Had the same problem with some pngs created in gimp. I solved it with:

ImageFile.LOAD_TRUNCATED_IMAGES = True

Why would this not be activated by default? Why would you have a library that fails when it could not fail?
I fail to see the benefit

Maybe there is Image encryption to make image not editable?

I met this problem here, the jpg file is visible on computer but can't edit or read its data.
kkmh_91672

Hello guys,
I am having the same problem while loading .tif file (with a shape = (36608, 186496), size = 6827245568 (6.83GB)) with PIL package. I am getting an PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fca1ff39ef0> error.

I don´t know to load it with a PIL or with any other packages. After loading it I want to compress it with a JPEG2000 format and later wants to save it back in TIF format.

So far, I have tried lots of packages but still, I couldn´t solve my problem. I even try imagicmagic but still I face the same problem.

Any help will be highly appreciated. (I wish I could upload an image file, but I couldn´t bcoz of file size).

Was this page helpful?
0 / 5 - 0 ratings