You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a bunch of gray-scale datasets what I would like to compress using the AVIF format to reduce the amount of data sent. Currently I'm able to store my grayscale data in an RGB AVIF file, but I want to use the grayscale compression ability of AVIF to hopefully get an even better compression ratio and not have to discard a bunch of extra data when loading the data file.
What did you expect to happen?
For the AVIF encoder/decoder to support gray-scale images.
What actually happened?
The AVIF encoder/decoder only supports RGB images.
What are your OS, Python and Pillow versions?
OS: Windows 10
Python: 3.12.10
Pillow: 12.1.1
im=PIL.Image.fromarray(img_data.astype(np.uint8)) # shape (nx, ny, 1)im.save("image.avif")
# This saves an RGB avif file instead of a grayscale file.
This saves an RGB avif file instead of a grayscale file.
This might sound like a silly question, but how do you know that? What tool are you using to inspect the file?
When I decode the file I get a 3 channel output. I mean it could be the decoder deciding it's 3 channels. But I also looked at the AVIF code in pillow and it explicitly does not handle grayscale data so I'm pretty certain it doesn't use the grayscale capabilities of avif, though admittedly I've not inspected the file itself.
While there seems to be an obvious way to designate an image as grayscale when encoding, rgb.format = AVIF_RGB_FORMAT_GRAY, I don't see an obvious way to determine if it is grayscale when decoding.
avifDecoder has the alphaPresent flag that we're currently using, but nothing else looks directly helpful. Indirectly, it has an image, but nothing in avifImage looks obvious either. image has depth, but an L mode image has the same depth as an RGB mode image, 8.
I started to wonder if it was even possible to tell, so I thought I'd ask what you were using to check.
I've not given up, I'm just letting you know where I'm at right now.
yuvFormat is a separate setting. If you want to use AVIF_PIXEL_FORMAT_YUV400, you can already with im.save("out.avif", subsampling="4:0:0")
I have a bunch of gray-scale datasets what I would like to compress using the AVIF format to reduce the amount of data sent.
If reducing file size is your ultimate goal, I don't think that AVIF_RGB_FORMAT_GRAY achieves it. Using img_data = np.ones([100, 100]), I ran a test in Pillow main, and created a 320 byte file. Testing with AVIF_RGB_FORMAT_GRAY, the result is the same.
However, im.save("out.avif", subsampling="4:0:0") does reduce the file size to 307 bytes.
What I'm saying is that YUV400 should be the default setting for grayscale images, and that when seeing YUV400 the decoder should produce a single channel "L" image as the result. This would be done by specifying AVIF_RGB_FORMAT_GRAY for the input texture, setting AVIF_PIXEL_FORMAT_YUV400 for the encoding. Then when loading, if the YUV format is AVIF_PIXEL_FORMAT_YUV400 then a single channel grayscale image should be read using AVIF_RGB_FORMAT_GRAY when decoding.
So say I have data with the shape (512, 512, 1), I want to encode it with AVIF_RGB_FORMAT_GRAY (for speed) with AVIF_PIXEL_FORMAT_YUV400 (for binary size) and then when I decode it I want to detect AVIF_PIXEL_FORMAT_YUV400 and use AVIF_RGB_FORMAT_GRAY (for speed) to convert that into data with the same (512, 512, 1) shape.
Currently I would get data with the shape (512, 512, 3) and a bunch of extra unnecessary work being done.
Activity
radarhere commentedon Mar 14, 2026
This might sound like a silly question, but how do you know that? What tool are you using to inspect the file?
NogginBops commentedon Mar 14, 2026
When I decode the file I get a 3 channel output. I mean it could be the decoder deciding it's 3 channels. But I also looked at the AVIF code in pillow and it explicitly does not handle grayscale data so I'm pretty certain it doesn't use the grayscale capabilities of avif, though admittedly I've not inspected the file itself.
radarhere commentedon Mar 14, 2026
And you're using Pillow as the decoder?
NogginBops commentedon Mar 15, 2026
Yes.
Here is the code that converts the image to RGB(A) before saving:
Pillow/src/PIL/AvifImagePlugin.py
Lines 248 to 253 in 3a44ba1
And here is the c code that selects the AVIF format to use when saving:
Pillow/src/_avif.c
Lines 506 to 510 in 3a44ba1
Here is the code that converts the decoded image to RGB(A):
Pillow/src/_avif.c
Lines 773 to 798 in 3a44ba1
radarhere commentedon Mar 15, 2026
Thanks.
While there seems to be an obvious way to designate an image as grayscale when encoding,
rgb.format = AVIF_RGB_FORMAT_GRAY, I don't see an obvious way to determine if it is grayscale when decoding.avifDecoder has the alphaPresent flag that we're currently using, but nothing else looks directly helpful. Indirectly, it has an
image, but nothing inavifImagelooks obvious either.imagehasdepth, but an L mode image has the same depth as an RGB mode image, 8.I started to wonder if it was even possible to tell, so I thought I'd ask what you were using to check.
I've not given up, I'm just letting you know where I'm at right now.
NogginBops commentedon Mar 15, 2026
AVIF_PIXEL_FORMAT_YUV400means grayscale (full-res luminance and no chrominance). So you could use that to detect grayscale.https://github.com/AOMediaCodec/libavif/blob/559c589b1f3d726c8051ce3e4c876b65b3d4c69a/include/avif/avif.h#L784
See:
AOMediaCodec/libavif#65
It's also worth noting that alpha is separate from color format so both color + alpha, gray + alpha and only alpha are also possible (though I only care about pure grayscale images without alpha).
https://github.com/AOMediaCodec/libavif/blob/559c589b1f3d726c8051ce3e4c876b65b3d4c69a/include/avif/avif.h#L279-L289
radarhere commentedon Mar 17, 2026
yuvFormatis a separate setting. If you want to useAVIF_PIXEL_FORMAT_YUV400, you can already withim.save("out.avif", subsampling="4:0:0")If reducing file size is your ultimate goal, I don't think that
AVIF_RGB_FORMAT_GRAYachieves it. Usingimg_data = np.ones([100, 100]), I ran a test in Pillow main, and created a 320 byte file. Testing withAVIF_RGB_FORMAT_GRAY, the result is the same.However,
im.save("out.avif", subsampling="4:0:0")does reduce the file size to 307 bytes.NogginBops commentedon Mar 17, 2026
What I'm saying is that YUV400 should be the default setting for grayscale images, and that when seeing YUV400 the decoder should produce a single channel "L" image as the result. This would be done by specifying
AVIF_RGB_FORMAT_GRAYfor the input texture, settingAVIF_PIXEL_FORMAT_YUV400for the encoding. Then when loading, if the YUV format isAVIF_PIXEL_FORMAT_YUV400then a single channel grayscale image should be read usingAVIF_RGB_FORMAT_GRAYwhen decoding.This would result in smaller binary images and avoid a bunch of work in libavif to detect and convert the color data while encoding/decoding.
Here is some code that explicitly uses
AVIF_RGB_FORMAT_GRAYto avoid computation: https://github.com/AOMediaCodec/libavif/blob/a1153dbccbdf005ce5b6632cdb2cf8be3c28d5ce/src/reformat.c#L248-L270The decoding also has fast-paths for converting
AVIF_PIXEL_FORMAT_YUV400toAVIF_RGB_FORMAT_GRAY.So say I have data with the shape
(512, 512, 1), I want to encode it withAVIF_RGB_FORMAT_GRAY(for speed) withAVIF_PIXEL_FORMAT_YUV400(for binary size) and then when I decode it I want to detectAVIF_PIXEL_FORMAT_YUV400and useAVIF_RGB_FORMAT_GRAY(for speed) to convert that into data with the same(512, 512, 1)shape.Currently I would get data with the shape
(512, 512, 3)and a bunch of extra unnecessary work being done.radarhere commentedon Mar 18, 2026
Ok, I've created #9471 to resolve this.
NogginBops commentedon Mar 18, 2026
That is perfect, thank you so much!