Rather Impressive Image Compression
Can you tell the difference between the two images above? At first glance, probably not. If you look at them side by side long enough, at least in fullscreen on an HD display, you might notice a little bit of a difference. In one the sky looks an ever so slightly darker shade, and if you look close enough, color shades seem to change in a more blocky structure instead of a more smooth transition. Zoom in, and you might notice the word “Excel” on the side of the windmill is mostly legible on one, and mostly illegible on the other.
The real difference, however, comes in the image’s size. The first one, completely unmodified from the camera, clocks in at 3,862,528 bytes - or 3.8 Megabytes. The second one, on the other hand, clocks in at a tiny 20,401 bytes, or 20.4 kilobytes - a whopping 0.005% of the original image’s size.
If this sounds too good to be true, or you want to inspect them more closely, you can download the files themselves here.
What dark art accomplished this?
The magical incantation of converting to AVIF.
JPG, as a file format, is 33 years old as of the Year of our Lord 2025. There’s been updates to the file format since then, but as this XKCD comic puts it, standards are sticky and slow to change. The AVIF file format, released in 2019 (and last updated in 2022), was designed to utilize existing video codec compression in still images. Utilizing better means of compression, and being able to utilize better processors in modern hardware to compress and render, gives quite the advantage to a more modern image format. AVIF isn’t the only modernized format; JPGXL (JPG’s successor) has some pros and cons over AVIF1, but since AVIF is supported in all major browsers and JPGXL isn’t, I’m sticking with AVIF for all online photos.
I am using some sleight of hand in the comparison; the compressed version is very heavily compressed. Doing a visibly lossless conversion (no details you can see are lost with the exception of some minor digital noise) brings the photo down to 185KB instead of 20.4KB, and this photo itself is a prime subject for compression (large sections with very few details). It’s still a good real world example, however. This isn’t some random photo I grabbed, I used this compressed photo for my previous blog post, and its 20.4KB size kept the page loading quickly and kept my blog’s files small.
As mentioned above, AVIF works in all modern browsers. Embed it on a page, just like I did above, and it works just fine. Beyond that, I’m pretty sure it’s natively supported by default on Windows, Mac, and many Linux desktops.
What I use AVIF images for
Despite its benefits, I don’t compress most of my photos for local storage. Compressing photos inherently removes data, data which could be used during future editing (or some crazy yet-to-be-developed AI tool a decade down the line). I keep my photo library fairly trimmed and organized, and having an extra 6GB on my drive is worth it.
I do, however, compress some photos for local storage. Random screenshots2 or informational photos (e.g. a photo of a filled out form) are always first on the chopping block. As long as the information I want to retain is retained, I might as well save the storage and convert those files.
I do occasionally compress regular photos as well, however. Usually it’s things that are mostly duplicates (e.g. keeping the best photo of a particular thing or scene, compressing the 2nd & 3rd best, then deleting the rest). Additionally, I usually export edited photos as AVIF, just with a higher quality setting than the one above. If I’ve still got the original raw or JPG I can always re-edit, so there’s no fear of losing hidden data in shadows or highlights. I usually edit in Darktable and Gimp, both of which support exporting in AVIF.
I mostly, however, use AVIF for sharing content on the web. Every photo in my photos page, as well as most of the photos in my blog posts, have all been compressed as AVIF. Big files take up a lot of space and bandwidth; a 1080p display can display a measly 2.07 megapixels, with 4k only being 8.3 megapixels. Most phones and cameras, including mine, are considerably above that, and turning gigabytes into a couple hundred megabytes is certainly worth the trade off.
How I compress photos
For single images, I generally use Squoosh.app. It’s a web app that’s capable of compression and conversion into many formats, and works offline. You can fine tune exactly how much compression, set a resolution, and balance size with quality in an intuitive way.
For conversion in bulk, I usually use libavif, a CLI tool for converting files in and out of AVIF format. While it’s not as fine tuned as Squooosh, if you’re uploading an entire library to the internet, or just imported screenshots from your phone and want to quickly delete some and then convert the rest, it’s really useful.
for file in *.jpg; do avifenc "$file" "${file%.jpg}.avif"; done
With the above command, you can create an AVIF version of any image in that particular directory. Just remember to replace “.jpg” with “.png” if you’re converting PNG files, or “.jpeg” if your photos use the alternative JPG file extension.
Wrap Up
It’s pretty impressive how much space you can save using something like AVIF. It definitely has its uses, particularly when sharing images online, and I’d implore you to check it out if you’re looking to share photos or are building some sort of platform and are looking to use a format that keeps the S3 bucket prices down.
Originally, I thought about posting this to my mini blog since I thought it was going to be shorter. However, like most of my posts, it kinda ballooned in size, and it also happens to be the normal kind of topic I’d post to my blog so I figured I’d post it here.
Speaking of which, though, I figured I’d share my miniblog again. I’m using it to experiment by writing a few shorter off-the-cuff posts, while posting them on alternative platforms and protocols. The posts won’t show up on my blog’s main feed, but have a separate RSS feed and a dedicated page for posts to keep me from cluttering up people’s RSS readers if they’re not interested in those posts.
Footnotes:
-
It’s actually been a bit heated of a debate - both sides have released studies claiming their format is more efficient. JPGXL supports a few features that AVIF does not, such as progressive loading, larger resolution limits, and lossless backwards compatibility (you could convert a .jpg to a .jxl, making it about 20% smaller, but at any point convert the new .jxl back into the original .jpg that has the same hash of the original .jpg file you started with). It’s also been alleged that Google intentionally squashed JPGXL, removing Chromium’s JPGXL support (maybe they’re just mad that everyone mildly disliked their format, webp). Nevertheless, AVIF is supported in more places, and is largely comparable to AVIF, so I generally use AVIF when I want to compress files. ↩︎
-
Converting screenshots, or any other PNG image, is even more effective. JPG is a lossy format, and already discards some data, even if it’s not as efficient as a more modern format. PNG is lossless, which means it doesn’t discard any data, making it a prime candidate for being converted to AVIF. ↩︎