如何將直向照片轉成 WhatsApp 貼圖(512×512 WebP,低於 100 KB)
How to Turn Vertical Photos into WhatsApp Stickers (512 512 WebP, Under 100 KB)
教學示範用 Python 和 Pillow 將直向照片置於 512×512 畫布,輸出 WhatsApp 可接受的 WebP 貼圖,並將靜態檔案控制在 100 KB 內。流程亦可選用 rembg 移除背景,再透過貼圖製作 App 匯入 WhatsApp。
Every phone gallery is full of tall photos — your cat mid-yawn, a birthday cake, a friend pulling a face. WhatsApp stickers are square. That mismatch is the entire job: get a 9:16 photo onto a 512×512 canvas without losing the subject, keep the file under 100 KB, and save it as a WebP that WhatsApp will actually accept.
In my last post I covered the transparent-WebP basics. This one is the photo-specific workflow — the exact steps I use to go from a portrait photo to a sticker in about two minutes, with Python and Pillow.
What WhatsApp actually checks
| Check | Requirement |
|---|---|
| Format | WebP (static or animated) |
| Dimensions | exactly 512 × 512 px |
| File size | ≤ 100 KB static (animated ≤ 500 KB) |
| Pack | up to 30 stickers, no mixing animated and static |
Miss any of these and the sticker importer rejects the file. These are WhatsApp's official requirements from their FAQ and their open-source sample project, so there's no version of "it worked on my phone" that skips them.
For tall photos the square canvas is the real problem, and you have two options:
- Crop — the photo fills the frame, but you lose the top and bottom (usually the subject's ears or feet).
- Pad — the whole photo survives, with transparent bands added on two sides.
For anything with faces, pad. A sticker that cuts off half a face gets saved exactly once.
Step 1 — fit the photo onto the square canvas
from PIL import Image
def to_canvas(src: str, dst: str) -> None:
canvas = Image.new("RGBA", (512, 512), (0, 0, 0, 0)) # fully transparent
photo = Image.open(src).convert("RGBA")
photo.thumbnail((500, 500)) # keeps aspect ratio, fits inside 500×500
x = (512 - photo.width) // 2
y = (512 - photo.height) // 2
canvas.paste(photo, (x, y), photo) # third arg = use alpha as mask
canvas.save(dst, "WEBP", quality=90)
to_canvas("portrait.jpg", "sticker.webp")
Two details that matter:
- Leave a few pixels of breathing room (hence 500, not 512) — stickers that touch all four edges look cramped in the picker.
-
paste(..., photo)uses the photo's alpha as a mask. Drop that third argument and you paste a black rectangle instead.
Step 2 (optional) — cut the background out
Padded photos look fine, but the classic sticker look is a floating subject. rembg does that in one line:
pip install rembg
from rembg import remove
photo = Image.open("portrait.jpg")
cutout = remove(photo) # first run downloads a ~170 MB model, then it's fast
If the cutout blends into dark chat backgrounds, give it a thin white edge first:
from PIL import ImageOps
cutout = ImageOps.expand(cutout, border=8, fill=(255, 255, 255, 255))
Then run the same to_canvas paste logic on cutout instead of the raw photo.
Step 3 — stay under 100 KB
WhatsApp's limit is 100 KB (102,400 bytes) per static sticker. WebP quality is the dial to turn:
from pathlib import Path
def save_small(canvas: Image.Image, dst: str) -> None:
for quality in range(90, 30, -10):
canvas.save(dst, "WEBP", quality=quality)
if Path(dst).stat().st_size <= 100 * 1024:
return
raise ValueError("still over 100 KB at quality 30 — shrink the image and retry")
Photos usually land between quality 60 and 80. If even quality 30 is too heavy, the photo is too busy — crop tighter or scale it down before padding.
Step 4 — get it into WhatsApp
The importer flow doesn't accept loose files, so use any sticker-maker app that imports your WebP files, or WhatsApp's own sample project if you want to self-host a pack. A pack of 3–30 stickers works best, and it shows up in your sticker tray.
One more thing: the same file doubles as a Telegram sticker — Telegram wants one side exactly 512 px and accepts PNG or WebP up to 512 KB. Make it once, use it twice.
Or skip the conversion entirely
If you'd rather grab stickers that already pass every check: FunnieStickers is a free library of 192 of them — all 512×512 WebP, all under 100 KB, no sign-up, click any sticker to download. The cat and Christmas sets are good starting points if you want something seasonal in the tray before you finish making your own.
Thanks for reading — feedback welcome in the comments.
來源:DEV Community · dev.to