"""Compile approved ImageGen atlases to ANM1; no artwork is synthesized here.""" import json import struct from pathlib import Path from PIL import Image ROOT = Path(__file__).resolve().parents[2] SRC = ROOT / 'assets/sprites/hall' OUT = ROOT / 'assets/animations/hall' PREVIEW = ROOT / 'build/underworld' def cells(name, cols, rows, count): im = Image.open(SRC / name).convert('RGBA') if im.getchannel('A').histogram()[0] == 0: raise ValueError(f'{name}: missing transparent background') return [im.crop((round(i % cols * im.width / cols), round(i // cols * im.height / rows), round((i % cols + 1) * im.width / cols), round((i // cols + 1) * im.height / rows))) for i in range(count)] def normalize(group, size, fill=.9): width,height = (size,size) if isinstance(size,int) else size bounds = [f.getchannel('A').point(lambda a: 255 if a >= 96 else 0).getbbox() for f in group] if any(b is None for b in bounds): raise ValueError('Empty generated frame') scale = min(width * fill / max(b[2]-b[0] for b in bounds), height * fill / max(b[3]-b[1] for b in bounds)) result = [] for f,b in zip(group,bounds): part = f.crop(b) part = part.resize((max(1,round(part.width*scale)),max(1,round(part.height*scale))),Image.Resampling.NEAREST) frame = Image.new('RGBA',(width,height)) frame.alpha_composite(part,((width-part.width)//2,round(height*.95)-part.height)) result.append(frame) return result def pack(name, frames, cols=4): w,h=frames[0].size payload=b''.join(f.tobytes('raw','BGRA') for f in frames) (OUT/name).write_bytes(struct.pack('<4sIIII',b'ANM1',w,h,len(frames),len(payload))+payload) sheet=Image.new('RGBA',(cols*w,((len(frames)+cols-1)//cols)*h),(24,29,32,255)) for i,f in enumerate(frames):sheet.alpha_composite(f,((i%cols)*w,(i//cols)*h)) sheet.save(PREVIEW/(name+'.png')) return {'frames':len(frames),'size':[w,h]} def main(): OUT.mkdir(parents=True,exist_ok=True);PREVIEW.mkdir(parents=True,exist_ok=True) portraits=cells('underworld-portraits-v2.png',4,3,11) portraits=[normalize([f],256)[0] for f in portraits] # Recruitment must show the same people as their existing in-game scenes. for slot,name in [(5,'bartender-v4.bin'),(6,'cabaret-ensemble-v1.bin'),(7,'fortune-room-v3.bin')]: data=(OUT/name).read_bytes() magic,w,h,count,size=struct.unpack('<4sIIII',data[:20]) assert magic==b'ANM1' frame=Image.frombytes('RGBA',(w,h),data[20:20+w*h*4],'raw','BGRA') portraits[slot]=normalize([frame],256)[0] world=cells('underworld-world-v2.png',4,2,8) world=normalize(world[:3],256)+[normalize([f],256)[0] for f in world[3:]] rats=cells('rat-race-v2.png',4,4,16) rats=sum((normalize(rats[i:i+4],(128,64)) for i in range(0,16,4)),[]) # Preserve the existing finish marker, now drawn as a small square instead of stretched. old=(OUT/'rat_race.bin').read_bytes() _,w,h,n,_=struct.unpack('<4sIIII',old[:20]) assert (w,h,n)==(128,128,17) rats.append(Image.frombytes('RGBA',(128,128),old[20+16*128*128*4:],'raw','BGRA').resize((128,64),Image.Resampling.NEAREST)) manifest={name:pack(name,frames) for name,frames in [ ('underworld-portraits-v2.bin',portraits),('underworld-world-v2.bin',world),('rat-race-v2.bin',rats)]} (OUT/'manifest-underworld-v2.json').write_text(json.dumps(manifest,indent=2)+'\n',encoding='utf-8') for index in range(4): loop=rats[index*4:index*4+4] loop[0].save(PREVIEW/f'rat-{index+1}.gif',save_all=True,append_images=loop[1:],duration=100,loop=0,disposal=2) world[0].save(PREVIEW/'semyon.gif',save_all=True,append_images=[world[1],world[2],world[1]],duration=450,loop=0,disposal=2) print(json.dumps(manifest,indent=2)) if __name__=='__main__':main()