← blog  /  tools

Convert images in Python with Pillow: WebP sizes, and the two bugs that ship

2026-11-27  ·  4 min read  ·  tools

On the 24 Kodak test photos, WebP at the quality of JPEG 80 was two thirds of the JPEG size, and lossless WebP about three quarters of an optimized PNG. A converter in 30 lines of Pillow, and the two bugs a quick one ships: transparent parts that turn black, and phone photos that turn sideways.

Converting an image with Pillow looks like two lines: Image.open(src).save(dst). It works until it meets a PNG with a transparent background or a photo from a phone. The function below handles both, resizes on the way, and picks the format from the file name.

On 1 November 2026 we ran it on the 24 Kodak test photos, the 768 x 512 set many compression tests use (Kodak released them for unrestricted use), and on 22 full-page screenshots of our own site. Quality is measured as PSNR, in decibels: higher is closer to the original.

The short version#

  • At the same quality number, WebP is smaller and closer to the original. Quality 80: JPEG 68 KB at 35.5 dB, WebP 49 KB at 36.2 dB (medians). The numbers mean different things in each format, so we also asked the other way round: WebP just as close to the original as JPEG 80 took 67 % of the JPEG's size (58 to 76 %, all 24 photos).
  • Lossless WebP beats optimized PNG: 72 % of the PNG's size on the photos, 77 % on the screenshots (27 to 92 %), with every pixel the same.
  • A transparent PNG saved as JPEG can turn its background black with .convert("RGB"): it does whenever the transparent pixels are stored as black. convert() puts them on white instead.
  • A phone photo stays sideways. Pillow opens the pixels as the camera stored them and does not apply the turn tag. Saved again without the tag, the photo shows sideways; ImageOps.exif_transpose() turns it first.

The code#

Convert, resize and flatten with Pillowpython
from PIL import Image, ImageOps

FORMATS = {"jpg": "JPEG", "jpeg": "JPEG", "png": "PNG", "webp": "WEBP", "gif": "GIF", "bmp": "BMP", "tiff": "TIFF"}
NO_ALPHA = {"JPEG", "BMP"}


def convert(src, dst, quality=80, background=(255, 255, 255), max_side=None, lossless=False):
    """Read `src`, turn it the right way up, shrink it to fit `max_side`, and save as the format `dst` ends with.
    Transparent parts go onto `background` when the new format has no transparency."""
    fmt = FORMATS[dst.rsplit(".", 1)[-1].lower()]
    img = ImageOps.exif_transpose(Image.open(src))     # apply the camera's turn tag (it is not kept)
    if max_side:
        img.thumbnail((max_side, max_side))
    if fmt in NO_ALPHA:
        if img.mode in ("RGBA", "LA", "PA") or (img.mode == "P" and "transparency" in img.info):
            rgba = img.convert("RGBA")
            flat = Image.new("RGB", img.size, background)
            flat.paste(rgba, mask=rgba.getchannel("A"))
            img = flat
        else:
            img = img.convert("RGB")
    options = {"JPEG": {"quality": quality, "optimize": True},
               "WEBP": {"quality": quality, "lossless": lossless, "method": 6},
               "PNG": {"optimize": True}}.get(fmt, {})
    img.save(dst, fmt, **options)
    return img.size
Run itpython
print(convert(photos[0], "kodim01.webp", quality=80), round(os.path.getsize("kodim01.webp") / 1024), "KB")
print(convert(photos[0], "kodim01_small.jpg", max_side=400), round(os.path.getsize("kodim01_small.jpg") / 1024), "KB")

It printed (768, 512) 87 KB for the first Kodak photo as WebP, and (400, 267) 28 KB for the same photo as a JPEG no wider than 400 pixels. thumbnail() keeps the shape of the picture and never makes it bigger.

The transparent background#

A PNG logo on a transparent background stores, for every pixel, a colour and how see-through it is. A fully see-through pixel still has a colour; in our test logo, as in any PNG that stores it as (0, 0, 0, 0), that colour is black. JPEG has no transparency, so .convert("RGB") keeps the colour and drops the rest: in our test logo the background came out as (0, 0, 0), black. convert() pastes the image onto a background of your choice, white by default, using the see-through value as the mask, and the corner came out (255, 255, 255).

The turn tag#

A phone camera often saves the pixels the way the sensor read them and adds a tag, EXIF Orientation, that says how to turn the picture for viewing. Photo apps read the tag; Pillow's Image.open() does not. Our test photo, stored 400 x 300 with the tag for a quarter turn, opened as 400 x 300, and saved as PNG it lost the tag, so every viewer showed it sideways. ImageOps.exif_transpose() turns the pixels and removes the tag, and convert() saved it as 300 x 400.

Which format#

  1. Photos for the web: WebP, quality 75 to 85. Every current browser shows it.
  2. Screenshots, diagrams, logos: lossless WebP, or PNG where WebP is not accepted.
  3. Files for people to open anywhere, or print: JPEG.

Many images at once#

For batches, our Image Converter on Apify converts and resizes images between PNG, JPEG, WEBP, GIF, BMP, TIFF and ICO, with the quality, fit modes, transparency handling and metadata stripping as settings, up to 1,000 a run. It is billed per image converted.

written