# -*- coding: utf-8 -*- from PIL import Image, ImageDraw src = r"D:\workspace\code\app\.workbuddy\hongbao.webp" img = Image.open(src).convert("RGBA") w, h = img.size tile = 16 # Panel 1: checkerboard background (shows transparency) def make_checker(width, height, t): cb = Image.new("RGB", (width, height), (255, 255, 255)) d = ImageDraw.Draw(cb) for y in range(0, height, t): for x in range(0, width, t): if (x // t + y // t) % 2 == 0: d.rectangle([x, y, x + t - 1, y + t - 1], fill=(204, 204, 204)) return cb checker = make_checker(w, h, tile).convert("RGBA") panel1 = Image.alpha_composite(checker, img).convert("RGB") # Panel 2: white background white_bg = Image.new("RGBA", (w, h), (255, 255, 255, 255)) panel2 = Image.alpha_composite(white_bg, img).convert("RGB") # Panel 3: alpha channel as grayscale (white = opaque, black = transparent) alpha = img.split()[3] panel3 = Image.merge("RGB", (alpha, alpha, alpha)) # Combine with labels gap = 10 label_h = 28 total_w = w * 3 + gap * 2 total_h = h + label_h canvas = Image.new("RGB", (total_w, total_h), (245, 245, 245)) canvas.paste(panel1, (0, label_h)) canvas.paste(panel2, (w + gap, label_h)) canvas.paste(panel3, (w * 2 + gap * 2, label_h)) d = ImageDraw.Draw(canvas) d.text((4, 6), "Transparent bg (checker)", fill=(0, 0, 0)) d.text((w + gap + 4, 6), "White bg", fill=(0, 0, 0)) d.text((w * 2 + gap * 2 + 4, 6), "Alpha channel", fill=(0, 0, 0)) out = r"D:\workspace\code\app\.workbuddy\hongbao_bg_compare.png" canvas.save(out) print("Saved:", out)