Convert and Manipulate AVIF Images with Pillow
This guide demonstrates how Python developers can process, modify, and convert modern AVIF (AV1 Image File Format) files using the Pillow library. You will learn how to enable AVIF compatibility within the Python imaging ecosystem, open and inspect AVIF files, apply standard manipulations such as resizing and rotation, and convert images seamlessly between AVIF, JPEG, and PNG formats.
Enabling AVIF Support
While Pillow provides extensive format support, AVIF encoding and
decoding typically rely on libavif or the pillow-heif
plugin for full cross-platform compatibility.
Install Pillow and pillow-heif via pip:
pip install Pillow pillow-heifBefore performing any file operations, register the AVIF plugin with Pillow at the start of your script:
from PIL import Image
import pillow_heif
# Register AVIF and HEIF openers with Pillow
pillow_heif.register_avif_opener()Once registered, standard PIL.Image functions handle
AVIF files natively.
Reading and Inspecting AVIF Images
Opening an AVIF file uses the standard Image.open()
workflow. You can inspect attributes such as format, dimensions, and
color mode directly:
from PIL import Image
import pillow_heif
pillow_heif.register_avif_opener()
with Image.open("example.avif") as img:
print(f"Format: {img.format}")
print(f"Size: {img.size}")
print(f"Mode: {img.mode}")Manipulating AVIF Images
Once loaded into memory, AVIF images behave like any other Pillow
Image object. You can resize, crop, filter, or rotate
them:
from PIL import Image, ImageFilter
import pillow_heif
pillow_heif.register_avif_opener()
with Image.open("input.avif") as img:
# Resize while maintaining aspect ratio
resized_img = img.resize((800, 600))
# Rotate 90 degrees counter-clockwise
rotated_img = resized_img.rotate(90, expand=True)
# Apply a blur filter
blurred_img = rotated_img.filter(ImageFilter.GaussianBlur(radius=2))
# Save the manipulated image back to AVIF
blurred_img.save("manipulated.avif", "AVIF")Converting Other Formats to AVIF
AVIF offers significantly higher compression efficiency compared to
JPEG and PNG. To convert standard image formats to AVIF, open the source
image and save it using the format="AVIF" parameter.
from PIL import Image
import pillow_heif
pillow_heif.register_avif_opener()
# Convert a JPEG or PNG to AVIF
with Image.open("source.png") as img:
# Convert RGBA to RGB if saving without transparency
if img.mode in ("RGBA", "P"):
img = img.convert("RGBA")
else:
img = img.convert("RGB")
img.save(
"output.avif",
format="AVIF",
quality=75, # Compression quality (0-100)
speed=6 # CPU effort (0 is slowest/best, 10 is fastest)
)Adjusting quality controls the balance between
compression and visual fidelity. The speed parameter
controls encoding performance, with lower numbers yielding smaller files
at the cost of processing time.
Converting AVIF to JPEG or PNG
To convert an AVIF file to another format for legacy browser support:
from PIL import Image
import pillow_heif
pillow_heif.register_avif_opener()
with Image.open("input.avif") as img:
# Convert to RGB if saving to JPEG (JPEG does not support alpha channels)
if img.mode in ("RGBA", "LA"):
rgb_img = img.convert("RGB")
rgb_img.save("converted.jpg", "JPEG", quality=90)
else:
img.save("converted.jpg", "JPEG", quality=90)
# Convert to PNG (preserves transparency)
img.save("converted.png", "PNG")