How to turn an AI image into an og:image link preview in Python

Ideogram 4.5 on Sume makes a 2:1 image at 1K as 1440x720. A Pillow crop to your link-preview size, plus the og:image:alt tag Open Graph asks for.

4 min readSume
All posts

Crop it, then resize it, and write the alt tag. A 2:1 image from Ideogram 4.5 on Sume does not match every link-preview template, so cut it to the ratio your template wants instead of stretching it. The code below does that with Pillow, and the tag block at the end covers the alt text.

The target size is yours to set. This post does not state any platform's preview size, because the platform's own help page is the source for that.

The sizes you start from

Ideogram 4.5 sizes on Sume, from the size table in the API code read 2026-10-06
Aspect ratioSize at 1KNote
1:11024 (2048 at 2K)Square
2:11440x720Edit override is 1408x704
3:1 / 1:33072x1024 / 1024x3072One size only
No aspect_ratio on an editSame geometry as the sourcePass the ratio only when you want it to change

Center-crop and resize

For a 1440x720 source and a 1200x630 target the crop keeps the full height and trims the sides. The function computes the crop box from the target ratio, so it also works for other sizes.

import sys
from PIL import Image

def og(src: str, dst: str, w: int = 1200, h: int = 630) -> None:
    im = Image.open(src).convert("RGB")
    cw = min(im.width, round(im.height * w / h))
    ch = min(im.height, round(im.width * h / w))
    left = (im.width - cw) // 2
    top = (im.height - ch) // 2
    box = (left, top, left + cw, top + ch)
    im.crop(box).resize((w, h), Image.LANCZOS).save(dst)
    print(f"crop {cw}x{ch} -> {w}x{h}")

if __name__ == "__main__":
    if len(sys.argv) != 3:
        raise SystemExit("usage: og.py SRC DST")
    og(sys.argv[1], sys.argv[2])

Compose before you crop

Center-cropping a 2:1 image to the template ratio removes the outer strip on each side, so keep the headline and the logo inside the middle of the frame. If you are generating the image, say so in the prompt: place the text in the central area with space to spare on both sides.

Check the crop by eye. A script cannot see that it cut a word in half.

The tag that goes with it

The Open Graph protocol page (read 2026-10-06) says og:image:alt should be specified if og:image is. Write what the image shows, and include any words printed on it.

<meta property="og:image" content="https://example.com/previews/launch.jpg">
<meta property="og:image:alt" content="Teal poster with the words Launch day in bold white type">

Sources

Related posts

More in Media tools

All Media tools posts

Written by Sume