fdintino / pillow-avif-plugin

A pillow plugin that adds avif support via libavif
BSD 2-Clause "Simplified" License
77 stars 12 forks source link

Error when open a HEIC image - Failed to decode image: Invalid ftyp #19

Closed RaphaelVRossi closed 1 year ago

RaphaelVRossi commented 1 year ago

I was testing some image format conversion on Thumbor, with pillow-avif-plugin.

And I guess I found a bug when pillow-avif try to open a HEIC file.

HEIC file tested here

# HEIC header
$ head -1 1.heic
ftypmif1mif1heic

AVIF file tested here

# AVIF header
$ head -1 image.avif
 ftypavifavifmif1miaf

Steps to reproduce

$ python3
# inside python
from PIL import Image
import pillow_avif
from io import BytesIO
import requests
body = requests.get("https://raw.githubusercontent.com/alexcorvi/heic2any/master/demo/1.heic").content
Image.open(BytesIO(body))

Log

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python3.9/site-packages/PIL/Image.py", line 3133, in open
    im = _open_core(fp, filename, prefix, formats)
  File ".../python3.9/site-packages/PIL/Image.py", line 3119, in _open_core
    im = factory(fp, filename)
  File ".../python3.9/site-packages/PIL/ImageFile.py", line 116, in __init__
    self._open()
  File ".../python3.9/site-packages/pillow_avif/AvifImagePlugin.py", line 61, in _open
    self._decoder = _avif.AvifDecoder(
ValueError: Failed to decode image: Invalid ftyp
fdintino commented 1 year ago

This is expected behavior. You can use pillow_heif to decode and encode HEIF files. It should be sufficient to add, where you have import pillow_avif:

import pillow_heif

pillow_heif.register_heif_opener()
RaphaelVRossi commented 1 year ago

I already tested with pillow_heif and pillow_avif together. Or I need to use only one pillow plugin at a time?

I was looking this code here and this validation should accept header with ftypmif1mif1heic?

    coding_brands = (b"avif", b"avis")
    container_brands = (b"mif1", b"msf1")
fdintino commented 1 year ago

Oh I see, yes that is a bug. I'm preparing the 1.3.0 release, I'll include a fix for that.

RaphaelVRossi commented 1 year ago

Thanks! @fdintino 🥳