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
Create a new Django project: django-admin startproject willow_avif_issue
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:
Install Willow with AVIF support in your Django project: pip install willow[heif]
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')
Run the management command: python manage.py test_willow
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:
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.
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
Create a new Django project:
django-admin startproject willow_avif_issue
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:Install Willow with AVIF support in your Django project:
pip install willow[heif]
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.Run the management command:
python manage.py test_willow
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. Theimage.avif
also retains the same EXIF orientation metadata. This tells viewers to rotate the image (again), resulting in a rotated 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
pip freeze
output