"""Build source-image inspection scales without adding analytical marks."""
from pathlib import Path
from PIL import Image, ImageOps, ImageDraw
import json, hashlib
O=Path(__file__).resolve().parent
V=Path('../../../resources/readers/provenance/source-4922c12a79c25bd2.html')
M=json.loads((V/'manifest.json').read_text())['items']
out=O/'views';out.mkdir(exist_ok=True)
records=[]
for idx in (2,113,120,121,122,126,127,128,129):
 c=M[idx];f=next((V/'images').glob(f'{idx:04d}_*.jpg'))
 image=Image.open(f)
 def emit(suffix,crop,edge):
    clip=image.crop(crop)
    clip.thumbnail((edge,edge),Image.Resampling.LANCZOS)
    name=f'{idx:04d}-{suffix}.jpg';clip.save(out/name,quality=94)
    records.append({'canvas':c['id'],'label':next(iter(c['label'].values()))[0],'source':str(f),
        'source_sha256':hashlib.sha256(f.read_bytes()).hexdigest(),'native_local_pixels':image.size,'crop_ltrb':crop,'view_pixels':clip.size,'view':name})
 w,h=image.size
 emit('sheet',(0,0,w,h),1250)
 if idx==2:emit('lower-lines',(120,1660,1920,2280),1450)
 if idx==113:emit('central',(250,120,1830,1700),1200)
 if idx in (120,121):
    for n in range(2):emit('area'+str(n+1),(n*w//2,0,(n+1)*w//2,h),1250)
 if idx==122:
    for n in range(3):emit('area'+str(n+1),(n*w//3,0,(n+1)*w//3,h),900)
 if idx==129:
    for n in range(4):emit('area'+str(n+1),(n*w//4,0,(n+1)*w//4,h),850)
(O/'IMAGE_VIEWS.json').write_text(json.dumps(records,indent=2)+'\n')
