fdintino / pillow-avif-plugin

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

Cannot load avif images #7

Closed paolobenve closed 2 years ago

paolobenve commented 2 years ago

I'm not certain, but I'd say that the plugin claims to have the ability to load avif images.

Actually I could save images to avif, but I couldn't load them. With some image pillow reports mime type image/heif, with others application/octet-stream.

Actually my mime database doesn't know image/avif, and I wonder if the reason why pillow doesn't load avif images is that it needs to know its mime type.

fdintino commented 2 years ago

This plugin does load AVIF images, and there are quite a few decoder tests that verifies the functionality. The mime gets registered with pillow here, so it shouldn't matter if it's missing from your system's mime database.

There are a few possible reasons why this plugin might not be loading images as you would expect:

  1. In order for this plugin to work, you'll need to have import pillow_avif somewhere in your application before PIL.Image.open. If that import is missing, or if it occurs after you've tried to open a file, it won't have registered itself with pillow.
  2. Another possible cause would be if you have libavif compiled without any decoder libraries. You can see a list of available codecs by running:
from pillow_avif import _avif
print(_avif.AvifCodecVersions())

If you are using the mac or manylinux wheels from pypi, this should output:

dav1d [dec]:0.9.2, aom [enc/dec]:3.1.3, rav1e [enc]:0.4.0 (0.4.0), svt [enc]:0.8.7

If you get an error trying to run the above lines that would be indicative of an issue either with the libavif installed on your machine or with how the C extension is compiled.

  1. I would triple-check that the images you're trying to load are indeed AVIF and not HEIF. When you register an image format with pillow, you pass an accept function to tell Pillow whether it should use your plugin to open a file. If bytes 4-12 of your file are ftypavif or ftypavif, then it is definitely an AVIF image. If instead you have ftypmif1 or ftypmsf1, pillow-avif-plugin would still attempt to open it, but there's a chance that the image is actually HEIF. And if it is, the extension won't throw an error to allow for future pillow HEIF support or for interoperability with a pillow HEIF plugin.
  2. Lastly, if you have other pillow image plugins installed it's possible that they could be pre-empting this one, depending on how they're written.
paolobenve commented 2 years ago

@fdintino Thank you for you answer!

you'll need to have import pillow_avif

I had commented it out, sorry

The images are now recognized, however, I'm getting the mime type with

import magic
self.mime_type = magic.detect_from_filename(media_path).mime_type

but since my libmagic doesn't know avif, I get the wrong value.

Have I a way to get the mime type correctly from PIL?

fdintino commented 2 years ago

I think you should be able to get them with im.get_format_mimetype()

paolobenve commented 2 years ago

thank you!