# 最初の画面の A・B・C 案の素材を作る（実機の動画の 15.5 秒のコマから）
#   A: 切り抜きを作り直し、赤いブロックだけ色を残す＋細かい点の地
#   B: 切り抜きの形を点だけで描く（ブロックは赤い点）
#   C: 写真をそのまま四角く置く（灰色＋ブロックだけ赤）
from rembg import remove, new_session
from PIL import Image, ImageDraw, ImageFilter
import numpy as np
from scipy import ndimage

# 1) 切り抜き（isnet-general-use。birefnet-general は 7GB のメモリで止まった）
src=Image.open('f_15.5.jpg').crop((400,0,900,500)).resize((1000,1000),Image.LANCZOS)
out=remove(src.resize((640,640)),session=new_session('isnet-general-use')).resize((1000,1000),Image.LANCZOS)
a=np.array(out).astype(np.float32)
al=a[...,3]; lum=0.299*a[...,0]+0.587*a[...,1]+0.114*a[...,2]
H,W=al.shape; yy,xx=np.mgrid[0:H,0:W]
al[(xx<262)&(yy<560)&(lum<110)]=0          # 三脚
al[(xx<262)&(yy<560)&(al<200)]=0
al[yy>700]=0                                  # 箱の影
m=al>110
lab,n=ndimage.label(m); sz=ndimage.sum(m,lab,range(1,n+1))
keep=np.isin(lab,1+np.argsort(sz)[-1:])       # 最大の塊だけ
keep=ndimage.binary_fill_holes(ndimage.binary_closing(keep,iterations=2)) & (m | ndimage.binary_erosion(keep,iterations=1))
soft=np.array(Image.fromarray((keep*255).astype(np.uint8)).filter(ImageFilter.GaussianBlur(1.2))).astype(np.float32)
a[...,3]=soft
arm=Image.fromarray(a.clip(0,255).astype(np.uint8),'RGBA')

def redkeep(img, region=None):
    A=np.array(img.convert('RGBA')).astype(np.float32); r,g,b=A[...,0],A[...,1],A[...,2]
    gy=0.299*r+0.587*g+0.114*b; mx=A[...,:3].max(-1); mn=A[...,:3].min(-1); sat=(mx-mn)/(mx+1e-6)
    rd=np.clip((r-np.maximum(g,b))/60,0,1)*np.clip((sat-0.35)/0.25,0,1)
    if region is not None: rd*=region(A.shape)
    out=A.copy()
    for i,c in enumerate([r,g,b]): out[...,i]=gy*(1-rd)+c*rd
    return Image.fromarray(out.clip(0,255).astype(np.uint8),'RGBA')
def blockregion(shape):
    H,W=shape[:2]; yy,xx=np.mgrid[0:H,0:W]; return ((xx<0.4*W)&(yy>0.5*H)&(yy<0.72*H)).astype(np.float32)

canvas=Image.new('RGBA',(840,852),(0,0,0,0)); canvas.paste(arm.resize((840,840),Image.LANCZOS),(0,0))
# A
redkeep(canvas,blockregion).save('pat/A_arm.webp','WEBP',quality=88)
S=4; sp=9; big=Image.new('RGBA',(840*S,852*S),(0,0,0,0)); d=ImageDraw.Draw(big)
for y in range(sp//2,852,sp):
  for x in range(sp//2,840,sp):
    rad=1.15*S; d.ellipse([x*S-rad,y*S-rad,x*S+rad,y*S+rad],fill=(75,95,109,90))
big.resize((840,852),Image.LANCZOS).save('pat/A_dots.png',optimize=True)
# B
a=np.array(canvas).astype(np.float32); al=a[...,3]/255; lum=0.299*a[...,0]+0.587*a[...,1]+0.114*a[...,2]
H,W=al.shape; yy,xx=np.mgrid[0:H,0:W]
mx=a[...,:3].max(-1); mn=a[...,:3].min(-1); sat=(mx-mn)/(mx+1e-6)
red=((a[...,0]-np.maximum(a[...,1],a[...,2]))>60)&(sat>0.5)&(xx<0.4*W)&(yy>0.5*H)
S=4; sp=6; big=Image.new('RGBA',(840*S,852*S),(0,0,0,0)); d=ImageDraw.Draw(big)
for y in range(sp//2,852,sp):
  for x in range(sp//2,840,sp):
    w=al[y-1:y+2,x-1:x+2].mean()
    if w<0.5: continue
    v=lum[y-1:y+2,x-1:x+2].mean(); t=1-v/255
    if red[y-1:y+2,x-1:x+2].mean()>0.4: col=(200,50,43,255); rad=2.7
    else: col=(30,34,40,255); rad=0.75+(t**0.9)*2.3
    rad*=S; d.ellipse([x*S-rad,y*S-rad,x*S+rad,y*S+rad],fill=col)
big.resize((840,852),Image.LANCZOS).save('pat/B_dots.png',optimize=True)
# C
ph=Image.open('f_15.5.jpg').crop((425,0,885,640))
A=np.array(ph.convert('RGB')).astype(np.float32); r,g,b=A[...,0],A[...,1],A[...,2]
gy=0.299*r+0.587*g+0.114*b; mx=A.max(-1); mn=A.min(-1); sat=(mx-mn)/(mx+1e-6)
rd=np.clip((r-np.maximum(g,b))/60,0,1)*np.clip((sat-0.35)/0.25,0,1)
H,W=gy.shape; yy,xx=np.mgrid[0:H,0:W]; rd*=((xx<0.5*W)&(yy>0.4*H)&(yy<0.7*H))
out=np.stack([gy*(1-rd)+c*rd for c in (r,g,b)],-1)
Image.fromarray(out.clip(0,255).astype(np.uint8)).resize((690,960),Image.LANCZOS).save('pat/C_photo.jpg',quality=90)
