Image Formats and Encoding

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Baseline date for this page: 2026-10-07. Browser support changes faster than the formats do, so confirm anything that matters on the MDN image type guide and Can I use before you drop a fallback.

A backend that accepts user uploads receives images in whatever format the user’s phone or editor produced, and has to send each client a format it can decode, at a size it needs, with as few bytes as possible. This page covers the formats and the encoder settings that decide the trade-off. The next pages cover variants and thumbnails and the per-language libraries.

The formats

Format Compression Transparency Animation Typical use Status and licence

JPEG

Lossy (DCT), 8-bit

No

No

Photos; universal fallback

Universally supported. Baseline patents long expired.

PNG

Lossless (DEFLATE), up to 16-bit

Yes (alpha)

APNG only

Screenshots, logos, line art

Universally supported. W3C standard.

WebP

Lossy (VP8) and lossless, 8-bit

Yes, also in lossy mode

Yes

General web images

Supported by current versions of all major browsers. BSD-style licence for libwebp.

AVIF

Lossy and lossless (AV1 intra frames), 8, 10 and 12-bit, HDR

Yes

Yes (image sequences)

Photos where size matters most

Supported by current major browsers. AV1 is royalty-free. Encoding is slow.

JPEG XL

Lossy (VarDCT) and lossless, can recompress JPEG losslessly

Yes

Yes

Archives, high quality

Browser support is uneven. Check before relying on it. Royalty-free.

HEIC / HEIF

Lossy (HEVC), 8 and 10-bit

Yes

Image sequences

Default photo format on iPhones

Patent-encumbered (HEVC). Native only on Apple platforms and some Android and Windows setups. Not a web delivery format.

HEIF is the container (ISO base media file format) and HEIC is HEIF with an HEVC-coded image. AVIF is also an HEIF container, with an AV1-coded image. That is why libheif reads both, and why a tool that handles AVIF can often be extended to HEIC with one more decoder library.

Why AVIF and WebP with a JPEG fallback is the default

  • AVIF usually gives the smallest file at equal visual quality for photos, WebP is a close second and is far cheaper to encode, and both are decoded natively by the browsers most visitors use.

  • A browser that supports neither still needs a picture, so one JPEG (or PNG when transparency matters) is always produced as the last source of a <picture> element, see native HTML images.

  • Content negotiation by Accept header (the server picks the format) and <picture> source lists (the browser picks the format) both work. The <picture> approach needs no server logic and caches cleanly at a CDN. The Accept approach needs a Vary: Accept header, or the CDN will serve the wrong format to some clients.

  • Encode cost is real. An AVIF encode at a slow speed setting can take seconds per image, so run it in a worker, see upload pipelines, not in the upload request.

Rule of thumb for what to generate per image:

  1. one AVIF variant (slow encode, smallest file);

  2. one WebP variant (fast encode, wide support);

  3. one JPEG variant (or PNG for graphics with transparency) as the fallback.

Quality settings and encoder trade-offs

Quality numbers are encoder-specific: JPEG quality 80 is not WebP quality 80 and neither is AVIF quality 80. Pick settings by looking at the output, then measure with an objective metric such as SSIM or VMAF (built for video, usable on stills).

Format Starting quality Notes

JPEG

75 to 85

Use a progressive scan and a modern encoder such as MozJPEG for smaller files at the same quality.

WebP

75 to 80 lossy

The method (effort) option trades encode time for size. Use lossless for graphics.

AVIF

50 to 65 in libvips and sharp, --min/--max quantiser 20 to 40 in avifenc

The speed or effort setting dominates encode time. Higher speed numbers mean faster, larger output in libaom and SVT-AV1.

PNG

not applicable

Lossless. Quantising to a palette (pngquant) shrinks files at a visible-quality cost.

Treat these as starting points to calibrate on your own images, not as recommendations from the encoder authors.

Other settings that change size more than people expect:

Chroma subsampling

JPEG usually stores colour at half resolution (4:2:0). That is invisible on photos and visible on red text over a white background. Use 4:4:4 for UI screenshots and keep 4:2:0 for photos.

Bit depth and HDR

AVIF and JPEG XL can carry 10-bit and HDR data. For web delivery of ordinary photos, 8-bit sRGB is the safe target.

Progressive vs. baseline JPEG

Progressive JPEGs render a blurry preview first. Against a baseline file with default Huffman tables they are smaller, but against a baseline file with optimised tables (optimize=True in Pillow, optimize_coding in libvips) the difference is within a few percent in either direction, see the measurements below. Use progressive for the visual effect on slow connections, and always optimise the tables.

Lossless vs. lossy

Use lossless only for graphics with flat colour. Lossless photos are several times bigger than lossy ones for no visible gain.

Worked example: the cover in every format

The running example of this section is a book cover, the Moby-Dick cover with the id b81f that the web pages use. The numbers below come from a generated 2400 by 3600 pixel stand-in (a sea gradient, a white shape and two lines of lettering, with a little grain) rather than a photograph, so the ratios between formats are indicative and will be smaller on real photos. They were measured with Pillow 13.0 (libwebp and libaom as bundled in its wheel), on one laptop, one run each. Sizes are exact for that input, times are orders of magnitude.

The ladder in three formats

The widths 320, 640 and 1280 are the ones the web pages ask for, with Pillow’s JPEG quality=80, WEBP quality=78 method=5 and AVIF quality=60 speed=6:

Width JPEG WebP AVIF

320 x 480

18,200 B

11,376 B

6,680 B

640 x 960

47,365 B

24,354 B

12,664 B

1280 x 1920

119,105 B

47,050 B

25,167 B

Whole ladder

184,670 B

82,780 B

44,511 B

The original is a 658,544 byte JPEG at quality 92 (5.5 MB as PNG), so the whole nine-file ladder is smaller than the original it came from, and the AVIF column alone is about 7 percent of it. The cost is the encode time, shown below.

The same quality at different sizes

Equal quality numbers are not equal quality, so compare formats at an equal objective score. The table sweeps each encoder at 640 by 960 and gives the structural similarity (SSIM, computed on the decoded RGB against the Lanczos-resized original with scikit-image):

Setting Bytes SSIM

JPEG quality 60

31,847

0.9629

JPEG quality 80

47,372

0.9754

JPEG quality 90

65,704

0.9838

WebP quality 60

18,604

0.9704

WebP quality 80

25,552

0.9753

WebP quality 90

37,824

0.9826

AVIF quality 40

7,703

0.9753

AVIF quality 60

12,690

0.9828

AVIF quality 70

15,287

0.9845

At an SSIM of about 0.975 the three formats need 47 KB (JPEG 80), 26 KB (WebP 80) and 7.7 KB (AVIF 40), which is why a mid-range AVIF quality of 40 to 60 is the usual starting point and why the same number means different things in each encoder. SSIM is a blunt tool: it favours smooth areas and says little about texture. Treat it as a way to find the neighbourhood and look at the files for the final choice.

What the effort and speed knobs cost

Encoder setting 1280 px bytes 1280 px time 2400 px bytes 2400 px time

JPEG quality 80, optimised, progressive

118,993

36 ms

332,608

100 ms

WebP quality 78, method 0

55,730

100 ms

115,718

170 ms

WebP quality 78, method 6

46,778

248 ms

95,300

851 ms

AVIF quality 60, speed 10

36,855

66 ms

67,500

93 ms

AVIF quality 60, speed 6

25,174

87 ms

46,274

428 ms

AVIF quality 60, speed 2

20,412

1,737 ms

36,620

5,019 ms

Both knobs trade time for bytes. Going from AVIF speed 10 to speed 2 halves the file and costs 25 to 55 times as long, and going from WebP method 0 to 6 saves 16 to 18 percent for two and a half to five times the time. The sweet spot is a middle setting (speed 6, method 4 to 5) for variants, and a slow setting only for images that are served millions of times. The numbers also show why AVIF belongs in a worker: a 2400 pixel AVIF at speed 2 takes five seconds on a laptop core, a JPEG takes a tenth of a second. AVIF speed and WebP method are encoder-specific names for the same trade-off, see upload pipelines for where to run it.

Limits to know

  • WebP cannot encode a side longer than 16,383 pixels: Pillow’s save raises ValueError: Image size exceeds WebP limit of 16383 pixels at 16,384.

  • JPEG is limited to 65,535 pixels per side (Pillow rejects anything above 65,500 with Maximum supported image dimension is 65500 pixels).

  • Subsampling costs bytes: the 640 pixel JPEG at quality 80 is 47,011 bytes with 4:2:0, 50,293 with 4:2:2 and 56,571 with 4:4:4 (20 percent more). Keep 4:2:0 for photographs and covers, and use 4:4:4 only for flat graphics saved as JPEG, where PNG or lossless WebP is usually the better answer anyway.

  • Progressive is not always smaller. With optimised Huffman tables the 320, 640 and 1280 pixel JPEGs were 18,414 / 47,011 / 115,719 bytes baseline against 18,213 / 47,372 / 118,993 progressive, so progressive trades a little size for the blurry first paint.

Choosing formats per image

flowchart TD S["Source image"] --> T{"Flat colour, text or transparency?"} T -- "yes" --> L["PNG or lossless WebP, plus a lossy WebP for large uses"] T -- "no, a photo or artwork" --> P{"Is the encode off the request path?"} P -- "yes" --> A["AVIF at speed 6, WebP method 4, JPEG fallback"] P -- "no, synchronous or on demand" --> W["WebP method 4 and JPEG, add AVIF later by backfill"] L --> O["Variants at 320, 640, 1280"] A --> O W --> O

Normalise on ingest

Every variant should come from the same normalised master, not from the raw upload. The order matters: orientation before resize (so width and height mean what the viewer sees), colour conversion before encode (so the numbers are sRGB), metadata removal last (so nothing re-adds it).

flowchart LR U["Upload: JPEG, PNG, HEIC, WebP"] --> D["Decode, check pixel count"] D --> R["Apply EXIF orientation to the pixels"] R --> C["Convert the colour profile to sRGB"] C --> F["Flatten or keep alpha"] F --> Z["Resize per width, never upscale"] Z --> E["Encode AVIF, WebP, JPEG"] E --> M["Write without EXIF, XMP or GPS"]

Orientation, measured

The phone-style test file stores its pixels as 3600 by 2400 with the EXIF Orientation tag set to 6 (rotate 90 degrees clockwise to display) and a GPS block. The eight possible values describe how the stored pixels map to the displayed image:

Orientation To display it, transform the stored pixels by

1

nothing

2

mirroring horizontally

3

rotating 180 degrees

4

mirroring vertically

5

transposing (mirroring along the main diagonal)

6

rotating 90 degrees clockwise

7

mirroring along the other diagonal (transverse)

8

rotating 90 degrees counter-clockwise

Values 5 to 8 swap width and height, which is why code that reads the header width must compare with the height for those orientations. What the tools do with the file:

  • vips thumbnail rotates by default and writes orientation 1: asked for 1280 it returns 853 by 1280 (the box rule above), and the GPS tags are still in the file unless keep=none is given.

  • Pillow: ImageOps.exif_transpose(im) returns a 2400 by 3600 image with the tag removed, and the files saved from it carry no EXIF at all.

  • Thumbnailator (Java) honours the tag by default and returns 640 by 960 for size(640, 960). With useExifOrientation(false) it returns 640 by 427 (the stored, landscape pixels), which is the sideways cover you get when a library ignores the tag.

Colour profiles, measured

A phone that shoots Display P3 stores pixel values that mean more saturated colours than the same numbers in sRGB. Treating them as sRGB makes them dull. With libvips (8.18, through pyvips) the P3 pixel (230, 80, 40) converts to the sRGB pixel (249, 63, 9), and the green (0, 200, 60) to (0, 204, 13). pyvips.Image.thumbnail(src, width, export_profile="srgb") (the --export-profile srgb of the command line) did the round trip on a P3-tagged JPEG: a swatch whose sRGB colour (230, 80, 40) had been stored as the P3 values (213, 91, 55) came back as (230, 80, 39). Without the export profile the same call returned (213, 91, 54) and kept the P3 tag. Pillow does the same with ImageCms.profileToProfile, shown in the Python page.

HEIC input needs a decoder

A libvips built from distribution packages can read HEIC only when the HEVC decoder is present. On Ubuntu 24.04 (libvips 8.15.1, libheif 1.17.6) the command above failed on a HEIC file with heif: Unsupported feature: Unsupported codec (4.3000) until the libheif-plugin-libde265 package was installed, and after that it wrote a 1200 by 1800 AVIF of 18,977 bytes from the 166,988 byte HEIC. The same distribution has no HEVC encoder, so heif-enc printed No HEVC encoder available: HEIC is an input format you can decode, not one you produce on a Linux worker. Pillow’s pillow-heif wheels bundle their own x265 and libde265, which is why they behave differently.

EXIF orientation, metadata and colour profiles

  • Orientation. Phones store the picture sensor-up and add an EXIF Orientation tag. Browsers honour it for <img> (CSS image-orientation: from-image is the default), but many processing libraries do not. Apply the rotation to the pixels during processing (libvips autorot, sharp rotate() with no arguments, Pillow ImageOps.exif_transpose) and then drop the tag, otherwise the output is rotated twice or not at all.

  • Metadata stripping. EXIF can hold GPS coordinates, device serial numbers and the photographer’s name. Strip it from every public variant, see security and operations. Keep the original file, with metadata, in private storage if you need it.

  • Colour profiles. Convert wide-gamut sources (Display P3 from iPhones, Adobe RGB from cameras) to sRGB for web variants, and either embed the profile or tag the file as sRGB. Without conversion, saturated colours look dull on clients that assume sRGB. Keep a profile only when you really target wide-gamut displays.

  • HEIC from iOS. iPhones upload HEIC by default unless the app asks for a compatible format. Convert to JPEG or AVIF on ingest, with a decoder built with libheif, and treat HEIC as an input format only.

Commands

libvips

libvips is the fastest general-purpose image library for server use, and the engine behind sharp, pyvips and NetVips. The command line tool is vips.

# Fit inside a box 1280 px wide (and as tall as needed), apply EXIF rotation, convert to sRGB, strip metadata, write an AVIF.
vips thumbnail input.heic output.avif[Q=60,effort=5,keep=none] 1280 --height 10000000 --export-profile srgb --size down

# The same as WebP and JPEG.
vips thumbnail input.heic output.webp[Q=78,keep=none] 1280 --height 10000000 --export-profile srgb --size down
vips thumbnail input.heic output.jpg[Q=80,optimize_coding,keep=none] 1280 --height 10000000 --export-profile srgb --size down

The size argument of vips thumbnail is the width of a box, and the height of the box defaults to the same number. Without --height, a 2400 by 3600 portrait cover asked for 1280 comes out 853 by 1280, because the height limits it first. To bound the width only, pass a very large --height (the pyvips example later uses the same trick). vips thumbnail keeps metadata by default, so the keep=none save option is what strips it (libvips 8.15 and later; older versions use strip instead, which 8.15 deprecated, and strip still works in 8.15.1 and in the 8.18 build bundled with pyvips 3.2). --size down stops it from enlarging images smaller than the target. vips thumbnail also shrinks on load where the format allows it, which is why it is much faster than loading the whole image first. Reference: libvips command line and the heifsave option list.

cwebp

cwebp -q 78 -m 5 -resize 1280 0 input.png -o output.webp
cwebp -lossless input.png -o output-lossless.webp

Reference: cwebp documentation.

avifenc

avifenc --min 20 --max 35 --speed 6 --jobs all input.png output.avif

avifenc ships with libavif, supplied by most Linux distributions as libavif-bin. Reference: libavif and the AV1 Image File Format specification.

JPEG XL

cjxl input.png output.jxl -d 1.0 -e 7
djxl input.jxl output.png

-d is the Butteraugli distance (0 means lossless, around 1 is visually lossless). Reference: jpeg.org JPEG XL and the libjxl repository. On the 2400 by 3600 cover (cjxl 0.7.0, -e 7), -d 1.0 wrote 198,402 bytes and -d 3.0 81,510 bytes. The format’s distinctive feature is lossless JPEG recompression: cjxl original.jpg out.jxl shrank the 658,544 byte cover JPEG to 446,847 bytes (32 percent smaller), and djxl out.jxl back.jpg restored a file that cmp reports as byte-identical to the original. That makes it attractive for archives of originals, whereas browser support still decides whether it can be a delivery format.

What the commands produced

The command line tools were run on the same cover (libvips 8.15.1, cwebp 1.3.2, avifenc 1.0.4 with libaom 3.8.2, cjxl 0.7.0, all from Ubuntu 24.04 packages):

Command Output

cwebp -q 78 -m 5 -resize 1280 0 on the 2400 x 3600 PNG

46,200 B (1280 x 1920)

cwebp -lossless on the PNG

4,050,366 B (the PNG is 5,529,505 B)

avifenc --min 20 --max 35 --speed 6 --jobs all

43,835 B, about 0.5 s wall time on 10 threads

vips thumbnail ... avif[Q=60,effort=5,keep=none] 1280 --height 10000000

20,226 B

vips thumbnail ... webp[Q=78,keep=none]

47,070 B

vips thumbnail ... jpg[Q=80,optimize_coding,keep=none]

115,454 B

Checking the result

Never assume a pipeline stripped what it should. Read the metadata back from the output, with vipsheader -a file | grep -iE 'orientation|gps|make' or with Pillow:

from PIL import Image

with Image.open("w640.webp") as im:
    assert not im.getexif(), "EXIF survived"
    assert im.size == (640, 960)

On the phone test file, vips thumbnail without keep=none kept exif-ifd0-Make, the orientation (rewritten to 1) and all four exif-ifd3-GPS* entries, and with keep=none the same command left none of them in the AVIF, WebP or JPEG output. Pillow outputs written without an exif= argument had none either.