Converting to AVIF with Python

힘센캥거루
2025년 10월 26일(수정됨)
2
59

After checking the blog with PageSpeed Insights, I found that it took a long time to load images.

I realized it was time for an image diet.

I originally used the webp format but discovered that AVIF offers higher compression rates, better compatibility, and even supports GIFs.

So, I decided to compare them.

Converting to AVIF with Python-1

After converting the png file to AVIF, the size was reduced by 10 times.

Two to three times is one thing, but 10 times is a lot.

I felt it had to be changed.

Converting to AVIF with Python-2

Using Python, it's possible to convert png to AVIF or vice versa.

First, install the necessary libraries using pip.

pip install pillow pillow_avif imageio

For now, my images are stored in paths like posts/[slug]/*.png.

Therefore, navigate to the path and convert all png files to AVIF.

from PIL import Image
import pillow_avif
import imageio
import pathlib

path = pathlib.Path.cwd()
# Check all files in the current path.
for folder in path.iterdir():
    if folder.is_dir():
        for file in folder.iterdir():
            # If it's a folder, check the files inside.
            # If the extension is jpg, png, etc., execute the conversion.
            if file.suffix in [".jpg", ".png", ".gif", ".PNG", ".JPG", ".jpeg", ".webp"]:
                img = Image.open(file)
                # If it's a gif file, it's definitely an animated image.
                # Save each image as a frame and convert.
                if file.suffix == ".gif" :
                    frames = []
                    frame_count = 0
                    try:
                        while True:
                            current_frame = img.convert('RGB')
                            frames.append(current_frame)
                            img.seek(img.tell() + 1)
                    except EOFError:
                        pass
                    imageio.mimsave(f'{file.parent}/{file.stem}.webp', frames, format='AVIF')
                # Otherwise, just convert and save the extension.
                else:
                    img.save(f'{file.parent}/{file.stem}.webp')

After conversion, a 5.5MB animated image became 500KB.

The compression rate is no joke.

Converting to AVIF with Python-3

Check the official Netflix blog for more on AVIF's performance.

The AVIF extension continues to be promoted by companies due to financial interests, so it will likely keep evolving.

Let's apply it to the blog and aim for SEO optimization as well.

관련 글

Book Review and Challenge Review of Chapter 7 of *Building an LLM from Scratch*
Book Review and Challenge Review of Chapter 7 of *Building an LLM from Scratch*
Chapter 7 covers the process of fine-tuning a model to follow instructions.In other words, making it give the desired response to a given question.As...
Review of Chapter 6 of *Build an LLM from Scratch*
Review of Chapter 6 of *Build an LLM from Scratch*
Chapter 6 is about fine-tuning for classification.The example used is building a spam classifier.A spam classifier determines whether something is spa...
Review of Chapter 5 of *Building an LLM from Scratch*
Review of Chapter 5 of *Building an LLM from Scratch*
Today is December 14.The challenge period actually ended two weeks ago, but I couldn’t just give up on writing a review.Because these TILs I leave lik...
Impressions After Reading Chapter 4 of “LLM From Scratch”
Impressions After Reading Chapter 4 of “LLM From Scratch”
Today is November 26, so if I finish one chapter a day, I’ll complete the challenge.I’m not sure if I can do it with my first and second kids constant...
Review of Chapter 3 of Learning LLM from Scratch
Review of Chapter 3 of Learning LLM from Scratch
After spilling a bucket of water on my MacBook, I was in shock and wasted about 3-4 days. In retrospect, since my MacBook was already damaged, I should have thought of it as being sent for repair and done something. Anyway, although it's a bit late, I am determined to see it through and leave a review of Chapter 3. 1. Attention Mechanism Chapter 3...
Review of Chapter 2 of Learning LLM from Scratch
Review of Chapter 2 of Learning LLM from Scratch
Already in the second week of the challenge. I hadn't finished Chapter 2 until yesterday, but while attending a two-day retreat, I managed to catch up by coding until midnight. 1. Content The main focus of Chapter 2 was tokenization, encoding, decoding, and embedding vectors. I was familiar with others as I had made a one-hot encoder, but embedding...

댓글을 불러오는 중...