Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ApqNBvNbNCwXqR2RLo8QJh
38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
"""Mette un quadro dentro una cornice disegnata col centro e il fuori in magenta pieno (#ff00ff): il magenta diventa
|
|
trasparente e il quadro riempie l'apertura senza deformarsi (si taglia quel che avanza). Così la tela resta
|
|
identica, e la cornice si può rifare a parte (prompt in prompts/personaggi/cornice-palio.txt).
|
|
python3 tools/incornicia.py assets/generated/cornice-palio.png assets/generated/palio-quadro.png assets/generated/palio-incorniciato.png"""
|
|
import sys
|
|
from PIL import Image
|
|
|
|
cornice, quadro, out = sys.argv[1:4]
|
|
f = Image.open(cornice).convert('RGBA')
|
|
W, H = f.size
|
|
px = f.load()
|
|
|
|
|
|
def magenta(p):
|
|
return p[0] > 200 and p[1] < 80 and p[2] > 200
|
|
|
|
|
|
# L'apertura: dal centro si va nei quattro versi finché c'è magenta.
|
|
cx, cy = W // 2, H // 2
|
|
l, r, t, b = cx, cx, cy, cy
|
|
while l > 0 and magenta(px[l - 1, cy]): l -= 1
|
|
while r < W - 1 and magenta(px[r + 1, cy]): r += 1
|
|
while t > 0 and magenta(px[cx, t - 1]): t -= 1
|
|
while b < H - 1 and magenta(px[cx, b + 1]): b += 1
|
|
for y in range(H):
|
|
for x in range(W):
|
|
if magenta(px[x, y]): px[x, y] = (0, 0, 0, 0)
|
|
p = Image.open(quadro).convert('RGBA')
|
|
ow, oh = r - l + 3, b - t + 3
|
|
k = max(ow / p.width, oh / p.height)
|
|
p = p.resize((round(p.width * k), round(p.height * k)), Image.LANCZOS)
|
|
x0, y0 = (p.width - ow) // 2, (p.height - oh) // 2
|
|
p = p.crop((x0, y0, x0 + ow, y0 + oh))
|
|
res = Image.new('RGBA', (W, H), (0, 0, 0, 0))
|
|
res.paste(p, (l - 1, t - 1))
|
|
res.alpha_composite(f)
|
|
res.crop(res.getbbox()).save(out)
|