wagtail / Willow

A wrapper that combines the functionality of multiple Python image libraries into one API
https://willow.wagtail.org/
BSD 3-Clause "New" or "Revised" License
273 stars 53 forks source link

AVIF image saved with incorrect EXIF orientation metadata after calling `auto_orient` #137

Closed Stormheg closed 9 months ago

Stormheg commented 10 months ago

When calling the auto_orient() method on a JPEG image with EXIF orientation metadata, the resulting image has the orientation applied directly to the image data. This works as expected.

But if the image is then saved using save_as_avif(), the (now incorrect) EXIF orientation metadata is encoded into the resulting AVIF file as well. This incorrectly tells viewers to rotate the image, resulting in a end user seeing a rotated image.

Steps to reproduce

  1. Create a new Django project: django-admin startproject willow_avif_issue

  2. Download and unzip image.jpeg.zip in the root of the django project created in the previous step. The extracted image.jpeg has EXIF orientation metadata. Here's a screenshot of the orientation metadata from macOS Preview:

    image
  3. Install Willow with AVIF support in your Django project: pip install willow[heif]

  4. Create a new management command (touch willow_avif_issue/management/commands/test_willow.py) with the following contents. Willow will throw an error if it is not being called from inside a configured Django project.

# willow_avif_issue/management/commands/test_willow.py
from django.core.management.base import BaseCommand
from willow.image import Image

class Command(BaseCommand):
    help = 'Test willow'

    def handle(self, *args, **options):
        with open('./image.jpeg', 'rb') as f:
            img = Image.open(f)

            img = img.auto_orient()
            img.save_as_avif('image.avif')
  1. Run the management command: python manage.py test_willow

  2. The resulting image.avif created in the project root should have the EXIF orientation directly applied to the image data. This can be verified by viewing the width and height. The image.avif also retains the same EXIF orientation metadata. This tells viewers to rotate the image (again), resulting in a rotated image:

image

Expected outcome

The EXIF orientation metadata is no longer correct and should probably be removed.

Notes

This specifically affects saving as AVIF. When saving as JPEG, it appears EXIF metadata is being (mostly) stripped. Other formats don't support orientation metadata at all I believe.

Technical details

robmoorman commented 9 months ago

If more input is needed please ask. We (Storm and I) have several use-cases now were this goes wrong in willow.

zerolab commented 9 months ago

This will be fixed by #136 which is on my list for this week. Will do my best to get to it ASAP