Assert an edited image has the source's pixel size: Python check

A short Python check that downloads the source and the edited file and fails if the pixel size differs. Needed on Sume whenever you send an aspect_ratio.

4 min readSume
All posts

Download both files and compare Image.size. If the edited file is not the same width and height as the source, stop the pipeline before it replaces a catalogue image. The check takes a dozen lines of Pillow and catches the case that matters most on Sume: you sent an aspect_ratio or resolution on an edit, and the model gave you a new shape.

When the size is expected to match

On Ideogram 4.5, an edit with no aspect_ratio and no image_size is sent with image_size: auto, so the shape of the source is kept (Image API docs). Ideogram's own reference says the output resolution matches the input exactly (read 2026-10-05). On GPT Image 2.5 you have to ask for the match with aspect_ratio: "auto", because leaving it out is not the same.

When you do send a shape, the size will differ by design. For example, aspect_ratio: "16:9" at 1K on an Ideogram 4.5 edit is 1536x864, and a resolution: "2K" with no ratio is ignored.

When to expect source size back (read 2026-10-05)
ModelRequestSize equals source?
Ideogram 4.5no aspect_ratio, no sizeyes
Ideogram 4.5resolution 2K onlyyes, 2K is ignored
Ideogram 4.5aspect_ratio 16:9no, 1536x864 at 1K
GPT Image 2.5aspect_ratio automatches the reference
GPT Image 2.5aspect_ratio omittednot guaranteed

The check

Run it after every edit in a batch, with the source URL and the result URL. It exits with code 1 on a mismatch, so a CI step or a shell loop stops.

import io, sys, requests
from PIL import Image

def size_of(url):
    r = requests.get(url, timeout=60)
    r.raise_for_status()
    return Image.open(io.BytesIO(r.content)).size

src_url, out_url = sys.argv[1], sys.argv[2]
src, out = size_of(src_url), size_of(out_url)
if src != out:
    print(f"size changed: {src} -> {out}")
    sys.exit(1)
print("ok", out)

What a mismatch should do

Do not resize the edit back to the source size. Stretching changes the shape of the product, and a crop can cut it off. Log the mismatch with the request body and, if you did not mean to send a shape, remove the field and run again. If you did mean it, run the crop step on purpose.

What this check does not prove

Matching size says nothing about whether pixels outside the edit stayed the same. For that, diff the two files, as in the Pillow diff post. A resized and a different-content image can both be the right size.

Where to put the check

Put it right after the download, before any upload or CMS write. A pipeline that checks only at the end can overwrite a good file with a bad one and leave you looking for the original.

In a batch, collect the mismatches in a list and print them at the end instead of stopping at the first one. You will usually find that all of them share one cause, such as a shape field added by a wrapper.

Sources

Related posts

More in Media tools

All Media tools posts

Written by Sume