PIL.UnidentifiedImageError: cannot identify image file
with open("photo.jpg", "rb") as fp:
print(fp.read(16))
What you see tells you what you really have:
| Starts with | It is |
|---|---|
b'\xff\xd8\xff' | A JPEG |
b'\x89PNG' | A PNG |
b'GIF8' | A GIF |
b'RIFF' then WEBP | A WEBP |
b'<!DOCTYPE' or b'<html' | A web page. Your download returned an error page |
b'{' | JSON, usually an API error message |
| Nothing, zero length | An empty file |
BytesIO that has already been read is at the end. Call fp.seek(0) first.pip install pillow-heif. Some TIFF and RAW variants need extra plugins.from io import BytesIO
from PIL import Image
buffer = BytesIO(data)
buffer.seek(0)
im = Image.open(buffer)
The PIL to Pillow converter finds most of these automatically, and the other error pages cover the rest.