"""Feather ONLY the outer botanical margin of the journal asset, keeping the
notebook body AND the bookmarks on the right edge fully solid (so they don't
get blurry). The fade is a thin gradient at the very edges so the botanical
bleeds through softly without a harsh boundary, but the notebook itself stays
crisp.
"""
from PIL import Image
import numpy as np

SRC = r'C:\Users\danam\SGT-FANTASTIC-08-06-26\Local\PixieSprout\assets\pixiesprout\notebook\journal-fullpage.png.bak-prefeather-20260811_134850'
OUT = r'C:\Users\danam\SGT-FANTASTIC-08-06-26\Local\PixieSprout\assets\pixiesprout\notebook\journal-fullpage.png'

im = Image.open(SRC).convert('RGBA')
w, h = im.size
a = np.array(im).astype(float)

# The notebook body spans roughly x 0.04..0.99, y 0.02..0.93.
# The bookmarks stick out on the RIGHT edge (x ~0.90..0.99) and must stay solid.
# So we only fade a THIN outer ring (the botanical bleed), ~3% from each edge,
# and keep everything inside fully opaque.
yn = np.linspace(0, 1, h)[:, None]
xn = np.linspace(0, 1, w)[None, :]

# distance from each edge (0 at edge, 1 at center)
edge_dist = np.minimum(np.minimum(xn, 1 - xn), np.minimum(yn, 1 - yn))

# Thin fade: only the outermost 3% fades to transparent; everything else solid.
# This keeps the bookmarks (which sit ~x 0.90-0.99, i.e. edge_dist ~0.01-0.10)
# mostly solid while the very outer botanical edge softens.
fade = 0.03
alpha = np.clip((edge_dist - 0.0) / fade, 0, 1)
alpha = alpha * alpha * (3 - 2 * alpha)  # smoothstep

# Force the notebook body fully opaque (edge_dist > 0.10 = inside the paper)
body = np.clip((edge_dist - 0.10) / 0.05, 0, 1)
body = body * body * (3 - 2 * body)
final_alpha = np.maximum(alpha, body)

a[:, :, 3] = final_alpha * 255
out = Image.fromarray(a.astype(np.uint8), 'RGBA')
out.save(OUT)
print('saved', OUT, out.size, 'mode', out.mode)
# verify: corner ~0, center 255, and a point on the right edge where bookmarks are
print('corner alpha:', out.getpixel((2, 2))[3])
print('center alpha:', out.getpixel((w // 2, h // 2))[3])
print('right-edge bookmark zone alpha (x=0.95w, y=0.5h):', out.getpixel((int(w*0.95), int(h*0.5)))[3])
