Skip to content

Support opening and saving L mode AVIF images with libavif >= 1.3.0#9471

Open
radarhere wants to merge 8 commits intopython-pillow:mainfrom
radarhere:avif_gray
Open

Support opening and saving L mode AVIF images with libavif >= 1.3.0#9471
radarhere wants to merge 8 commits intopython-pillow:mainfrom
radarhere:avif_gray

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Mar 18, 2026

Resolves #9465

If an AVIF image is opened with YUV 4:0:0 subsampling, then it is grayscale, so we can use AVIF_RGB_FORMAT_GRAY, instead of AVIF_RGB_FORMAT_RGB, to load it in L mode rather than RGB. Similarly, if the frame being saved is grayscale, using AVIF_RGB_FORMAT_GRAY simplifies encoding within libavif. However, AVIF_RGB_FORMAT_GRAY only become available in libavif 1.3.0, so this feature is only present for that version. As a side effect, this also means that if an RGB image is saved with 4:0:0 subsampling, it will be in L mode after reopening.

Also, if all frames being saved to an AVIF image are grayscale, then if we default to 4:0:0 subsampling (instead of the current default of 4:2:0), then together, an L mode image can be saved and opened again in the same mode.

@radarhere radarhere changed the title Support opening and saving L mode AVIF images for libavif >= 1.3.0 Support opening and saving L mode AVIF images with libavif >= 1.3.0 Mar 19, 2026
radarhere and others added 2 commits March 26, 2026 08:49
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
@radarhere
Copy link
Member Author

It turns out that Valgrind is raising an error - https://github.com/python-pillow/Pillow/actions/runs/23566085263/job/68628338690?pr=9471#step:5:6558

I've created AOMediaCodec/libavif#3139 to address it.

@radarhere
Copy link
Member Author

My PR was merged, but their analysis of the problem is that while the uninitialized value is copied to a local variable, it isn't actually used.

So I suppose there's a choice here.
a) wait for a new release of libavif
b) wait for a new release of libavif, and restrict this feature to that new version or newer
c) avoid waiting and just silence the valgrind error on this test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support monochrome AVIF saving and loading

2 participants