
If you publish a photo gallery, try AVIF on a representative photo. If you publish diagrams or screenshots with small type, include lossless WebP in the first test. Under a 40 KiB limit, our 512-pixel photo came out at 37.4 KiB in AVIF and 38.3 KiB in WebP. The difference was modest. The bigger saving came from switching the text graphic to lossless WebP, which kept every pixel in just 8.3 KiB.
The cover shows enlarged crops from the original PNGs and the selected lossy encodes. The two file sizes below the photo are for the complete photograph. The source is NASA’s public-domain portrait of astronaut Eileen Collins.
Both modern image formats support transparency and lossy compression. Compare the same image at its final display dimensions to judge visual quality. A quality setting of 75 has a different meaning for each encoder. If a design allows 40 KiB for a thumbnail, give each encoder that byte budget and let it use whichever quality setting produces its best qualifying output.
AVIF stores images encoded with AV1 and can carry high-bit-depth and high dynamic range content. WebP has broad browser support and is often a practical choice for on-demand previews. Both AVIF and WebP support animated images. If your inputs include HDR or animation, build a separate test that preserves the color information or measures the complete sequence. Our results apply to static 8-bit images. AVIF specification · Google’s WebP format guide
Start from the same source at the same dimensions. A resize, a change in color profile, or a second round of JPEG compression can otherwise dominate the differences you are trying to measure. Our test used four 8-bit PNG inputs: a 512 × 512 photograph and three 540 × 360 graphics. The Pillow batch-resizing guide covers preparing consistent dimensions before encoding.
We tried seven quality settings in Pillow 13.0.0 with libavif 1.4.2 and libwebp 1.6.0. For each format, we discarded files over 40,960 bytes and any file that changed the alpha channel. Of the remaining files, we selected the one with the lowest measured pixel error. These are the actual output sizes.
The alpha result applies to the encoder settings we tested. AVIF supports transparency. Exact preservation of every opacity value is a stricter requirement, useful for a logo that will sit on several backgrounds. For a photo that fills its frame, there is no transparent edge to protect.
Install a Pillow build with AVIF and WebP support, save one representative input as source.png, and run this Python example in a fresh directory. It writes each candidate and prints the files that meet both limits. Check the Pillow encoder options when comparing different library builds.
from pathlib import Path
from time import perf_counter
from PIL import Image
source = Image.open("source.png").convert("RGBA")
for fmt in ("AVIF", "WEBP"):
for quality in (35, 45, 55, 65, 75, 85, 92):
path = Path(f"sample-{quality}.{fmt.lower()}")
start = perf_counter()
source.save(path, format=fmt, quality=quality)
elapsed_ms = (perf_counter() - start) * 1000
with Image.open(path) as decoded:
alpha_ok = (decoded.convert("RGBA").getchannel("A").tobytes()
== source.getchannel("A").tobytes())
if path.stat().st_size <= 40 * 1024 and alpha_ok:
print(path, path.stat().st_size, round(elapsed_ms), "ms")
For logos and small lettering, try lossless compression as a separate candidate. WebP supports both lossy and lossless compression. A graphic with large areas of flat color can be smaller in lossless mode even when lossy compression works well for photographs. To generate the control file, run these two lines after importing Pillow.
with Image.open("source.png") as image:
image.save("sample-lossless.webp", lossless=True, method=4, exact=True)
Inspect the qualifying files at their intended display size, then enlarge an identical crop from each. Look at fine lettering, hair, sharp diagonals, and grain. Put a transparent image on both a light and a dark background. A fringe that disappears against white can stand out on a dark navigation bar.
For the numeric comparison, we decoded each candidate, composited it and the source onto white and dark backgrounds, and measured the root-mean-square difference across their RGB channels. The larger of the two errors determined the ranking. A lower error means the pixels were closer under this test. Your readers may still prefer the smaller file if they cannot see the difference at display size.
Once you have chosen the best version of each image, browser support determines which files you need to deliver. The HTML picture element lets you offer AVIF images to browsers that support AVIF, WebP images as a second choice, and a conventional JPEG for the photo fallback. The browser selects a compatible source in document order. It does not compare file sizes or image quality for you.
<picture>
<source type="image/avif" srcset="/images/photo.avif">
<source type="image/webp" srcset="/images/photo.webp">
<img src="/images/photo.jpg" width="512" height="512"
alt="Astronaut Eileen Collins in an orange flight suit">
</picture>
Put the format you want to serve first. For the small-type graphic in this test, that would be lossless WebP. Use PNG for the fallback if you need transparent pixels. A JPEG fallback suits an opaque photograph. Confirm the selected URL in your browser’s network panel and test the browsers or embedded views your audience actually uses. The MDN picture element reference explains source selection.
A gallery that changes once a month can amortize a slower encode over thousands of views. A service that generates a preview on every upload has to make the user wait for that first result. We timed three in-memory encodes per setting and reported the median. The short example above also writes the file, so its elapsed time includes that write. Neither timing measures browser decoding speed.
For our photo, AVIF saved 988 bytes per delivery at an extra 33 ms of encode time. If the same two files are each requested 10,000 times, the smaller file saves about 9.4 MiB of transfer. If a one-off preview is viewed once, the saving is still just 988 bytes. Use the expected reuse of the image to decide whether the extra encoding time is worthwhile.
For a production estimate, encode 100 representative images and record elapsed time for the whole batch, total delivered bytes, and how many images failed the budget. Run the same inputs on the CPU instance with Hivenet you are considering, with the concurrency you intend to use. That gives you a basis for sizing a recurring job without extrapolating from one photograph.
Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.