Converting to AVIF with Python

힘센캥거루
2025년 1월 13일(수정됨)
2
58

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}.avif', frames, format='AVIF')
                # Otherwise, just convert and save the extension.
                else:
                    img.save(f'{file.parent}/{file.stem}.avif')

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.

관련 글

Automating School Work – Using AI to Check Subject-Specific Remarks in Student Records
Automating School Work – Using AI to Check Subject-Specific Remarks in Student Records
If I had to pick the most meaningless, exhausting, and boring task at school, I would choose checking student records.In middle school, the student re...
Python OCR Recommendations for MacBook Users
Python OCR Recommendations for MacBook Users
It seems like I've tried every OCR available for recognizing students' medical certificates. I've used various OCRs such as Tesseract, EasyOCR, and PaddleOCR, but none had satisfactory performance with Korean. Recently, however, I discovered a Python library that wraps the Live Text functionality available on MacBook...
Automatic Face Mosaic Using YOLO
Automatic Face Mosaic Using YOLO
I've been writing café and restaurant blogs for a while, and it was too tedious to mosaic faces every time I uploaded photos. Naver and Tistory also support automatic mosaics, but if you have 30 photos, you have to click each one, and the recognition rate wasn't great. So I decided to just make it myself...
Implementing Python Multithreading (Parallel Execution)
Implementing Python Multithreading (Parallel Execution)
When coding with Python, you often encounter issues when implementing a GUI like tkinter. When a button is pressed to execute a specific function, the GUI freezes. So, I decided to solve this using multithreading. 1. Multithreading? Multiprocessing? Multithreading is within a single process...
Can we crawl the K-Business Portal?
Can we crawl the K-Business Portal?
Recently, I noticed that the K-Business Portal displays official document titles in segments of three months. What if we could collect all document titles through crawling and compile three years' worth into an Excel file? It seemed like using a filter to search document numbers would be easy. To cut to the chase, it was impossible....
School Task Automation - Automating Nighttime Self-Study Attendance
School Task Automation - Automating Nighttime Self-Study Attendance
Computerizing attendance checks using Python and spreadsheets

댓글을 불러오는 중...