From a folder of photos to 190 stickers: a Pillow pipeline that hits every app's size limit
Sticker libraries look like a design problem and turn out to be a file-format problem. Every chat app invents its own rules, and the failure modes are silent: WhatsApp greys out a sticker that is 3 KB too large, Discord
Sticker libraries look like a design problem and turn out to be a file-format problem. Every chat app invents its own rules, and the failure modes are silent: WhatsApp greys out a sticker that is 3 KB too large, Discord rejects a file for being 408 px wide when it was 512, Telegram publishes a pack with invisible white boxes because the PNG lost its alpha channel somewhere in the pipeline.
This is the batch pipeline I use to turn a folder of source images into a library that installs cleanly on all three apps — about 120 lines of Python, no Photoshop, no manual export.
The constraints that drive the design
Three numbers shape everything:
- WhatsApp — 512 × 512 px, WebP, ≤ 100 KB for static stickers
- Telegram — one side exactly 512 px, PNG or WebP, ≤ 512 KB
- Discord — 408 × 408 px, PNG or APNG, ≤ 512 KB
The 100 KB WebP ceiling is the only genuinely hard one. Telegram gives you five times that budget, and Discord's 512 KB is generous. So the pipeline targets WhatsApp first and derives the other two from the same master.
Design decisions that follow from that:
- Keep one master PNG at 512 × 512 with real transparency — the source of truth
- Encode WebP adaptively: binary-search the quality factor until the file fits, instead of picking
quality=80and hoping - Derive the Discord variant by downscaling to 408, never upscaling from something smaller
- Pad the artwork inside the canvas so nothing touches the edge — chat apps add their own padding and a full-bleed sticker looks cropped
Step 1 — Normalise every source to a 512 square
The first pass does four things: fix EXIF rotation, strip background (optional), put the subject on a transparent square canvas with a margin, and save the master PNG.
from pathlib import Path
from PIL import Image, ImageOps
CANVAS = 512
MARGIN = 0.06 # 6% breathing room on each side
def to_square(src: Path, cutout: bool = False) -> Image.Image:
im = Image.open(src)
im = ImageOps.exif_transpose(im).convert("RGBA")
if cutout:
from rembg import remove # pip install rembg
im = remove(im)
bbox = im.getbbox() # crop to the actual subject
if bbox:
im = im.crop(bbox)
inner = int(CANVAS * (1 - 2 * MARGIN))
im.thumbnail((inner, inner), Image.LANCZOS)
canvas = Image.new("RGBA", (CANVAS, CANVAS), (0, 0, 0, 0))
canvas.alpha_composite(
im, ((CANVAS - im.width) // 2, (CANVAS - im.height) // 2)
)
return canvas
exif_transpose matters more than it looks: phone photos carry an orientation flag, and without it a landscape shot of a portrait subject lands sideways in the sticker. thumbnail preserves aspect ratio, so a wide image fits the square instead of being squashed.
Step 2 — Hit the 100 KB WebP ceiling without guessing
Fixed quality settings fail in both directions: quality=90 blows past 100 KB on detailed artwork, quality=60 throws away quality on artwork that had room to spare. Binary-search it:
from io import BytesIO
def encode_webp(im: Image.Image, budget: int = 100 * 1024) -> bytes:
lo, hi, best = 1, 95, None
while lo <= hi:
mid = (lo + hi) // 2
buf = BytesIO()
im.save(buf, "WEBP", quality=mid, method=6, exact=True)
data = buf.getvalue()
if len(data) <= budget:
best = data # fits — try to go higher
lo = mid + 1
else:
hi = mid - 1 # too big — step down
if best is None:
raise ValueError("cannot fit budget even at quality=1")
return best
Two flags earn their place:
-
method=6is the slowest, best-compressing encoder pass. For a batch job you run once, it is free quality. -
exact=Truekeeps RGB values in fully transparent pixels. Without it libwebp rewrites them, and a sticker with a soft drop shadow can develop a dark halo when a chat app renders it against a light theme.
In practice a flat cartoon sticker lands at quality 80–92 and a photographic cutout near 55–70, both under 100 KB, and neither wastes bytes.
Step 3 — Derive the other two formats
def export_all(master: Image.Image, stem: str, out: Path):
out.mkdir(parents=True, exist_ok=True)
# WhatsApp: WebP, adaptive quality, hard 100 KB cap
(out / f"{stem}-whatsapp.webp").write_bytes(encode_webp(master))
# Telegram: same 512 canvas, PNG is fine at this size
master.save(out / f"{stem}-telegram.png", optimize=True)
# Discord: real downscale to 408, not a resize of the WebP
discord = master.resize((408, 408), Image.LANCZOS)
discord.save(out / f"{stem}-discord.png", optimize=True)
The Discord step is worth calling out because it is the most common mistake in older tutorials: Discord required 320 × 320 until 2023 and now wants 408 × 408. A 320 file still uploads on some clients and renders soft, which makes the bug easy to miss.
What actually bites in a batch job
Five failures covered the first few hundred images:
- PNG in the WebP slot. WhatsApp accepts the upload, then shows a grey placeholder. The format, not the size, was wrong.
-
thumbnail()on a 1-pixel image. Tiny sources produce a sticker that is 1 px of subject in a 512 canvas. Skip anything under 200 px. -
EXIF rotation. Half a folder arrives sideways until
exif_transposeis in the pipeline. - Alpha loss through a round trip. Any step that saves as JPEG in the middle silently drops transparency; keep everything RGBA PNG until the final encode.
- Identical output for different inputs. Deduplicate by perceptual hash, not filename — the same meme saved twice at different sizes is one sticker, and near-duplicates make a library feel padded.
Where this ends up
The output of this pipeline is the sticker library behind FunnieStickers — every file is a 512 × 512 WebP with a transparent background, sized to install in WhatsApp, and the same artwork exports to PNG for Telegram and 408 px for Discord. Browsing the meme collection or the Christmas stickers shows the finished result: no background boxes, no cropped artwork, consistent padding across a few hundred files.
If you are building your own set, start with the encoder loop. Getting the 100 KB budget under automated control is what turns a folder of images into something you can regenerate tomorrow without re-checking every file by hand.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.