Home / Errors / cannot-identify-image

Cannot identify image file

Pillow does not recognise the file as an image. Nine times out of ten the file is not what its name says it is.

PIL.UnidentifiedImageError: cannot identify image file

Look at the first bytes

with open("photo.jpg", "rb") as fp:
    print(fp.read(16))

What you see tells you what you really have:

Starts withIt is
b'\xff\xd8\xff'A JPEG
b'\x89PNG'A PNG
b'GIF8'A GIF
b'RIFF' then WEBPA WEBP
b'<!DOCTYPE' or b'<html'A web page. Your download returned an error page
b'{'JSON, usually an API error message
Nothing, zero lengthAn empty file

The usual causes

Reading from bytes properly

from io import BytesIO
from PIL import Image

buffer = BytesIO(data)
buffer.seek(0)
im = Image.open(buffer)

Still stuck

The PIL to Pillow converter finds most of these automatically, and the other error pages cover the rest.