from PIL import Image
import os

# We treat the navy-blue dragon-mark.png as a silhouette source: anything with
# nontrivial alpha is "dragon", everything else stays transparent. We emit a
# pure-black and pure-white variant, tightly cropped to the actual content
# bbox so they center cleanly when used as a watermark.
ROOT = os.path.dirname(os.path.abspath(__file__))
src_path = os.path.join(ROOT, "dragon-mark.png")
src = Image.open(src_path).convert("RGBA")

alpha = src.split()[-1]
# Threshold to drop anti-alias fringe that can fool getbbox into reporting
# the whole canvas as "in".
mask = alpha.point(lambda v: 255 if v > 16 else 0)
bbox = mask.getbbox()
print("content bbox =", bbox, "size =", (bbox[2] - bbox[0], bbox[3] - bbox[1]))

cropped = src.crop(bbox)
_, _, _, ca = cropped.split()

for name, rgb in (("black", (0, 0, 0)), ("white", (255, 255, 255))):
    flat = Image.merge(
        "RGBA",
        (
            Image.new("L", cropped.size, rgb[0]),
            Image.new("L", cropped.size, rgb[1]),
            Image.new("L", cropped.size, rgb[2]),
            ca,
        ),
    )
    out_path = os.path.join(ROOT, f"dragon-mark-{name}.png")
    flat.save(out_path, "PNG", optimize=True)
    print("wrote", out_path, flat.size)
