"""python tree_reverse_372.py EVIDENCE CARRIER OUTPUT. Reverse search in the full 2016 image."""
from pathlib import Path
from PIL import Image
import numpy as np,json,sys
from tree_search_371 import scores,top
def main(d,c,o):
 im=np.array(Image.open(c).convert('L'),float)
 # Session 18 rectangle fixed well before this experiment. No poster location supplied here.
 src=Image.open(d/'49.jpg').convert('L').crop((640,2020,1770,3200))
 obs={'raw':im/255,'clip8':np.minimum(im,8)/8,'clip16':np.minimum(im,16)/16,'clip32':np.minimum(im,32)/32}
 out={}
 for lab0,arr in obs.items():
  for profile in ('relative','half_raw_level'):
   cap={'raw':255,'clip8':8,'clip16':16,'clip32':32}[lab0];floor=.015 if profile=='relative' else .5/cap
   lab=lab0+'_'+profile
   runs=[]
   for w in range(120,206,10):
    h=round(w*1180/1130);q=1-np.array(src.resize((w,h),Image.Resampling.BOX),float)/255
    s=scores(arr,q,floor);r,x,y=top(s)
    runs.append([round(r,6),x,y,w,h])
   out[lab]={'best':max(runs),'per_width':runs,'normalized_std_floor':floor}
 obj={'reference_box':[640,2020,1770,3200],'full_poster_size':list(im.shape[::-1]),'results':out,'scores_for':'positive correlation with the inverted book ink; fixed src aspect, tested widths 120..200 in steps of 10; no mask for text'};o.write_text(json.dumps(obj,sort_keys=True,indent=2)+'\n')
if __name__=='__main__':main(*map(Path,sys.argv[1:]))
