23 lines
1.2 KiB
Python
23 lines
1.2 KiB
Python
"""Preserve generated originals and export responsive WebP assets."""
|
|
from pathlib import Path
|
|
from PIL import Image,ImageOps,ImageDraw
|
|
import json,shutil
|
|
ROOT=Path(__file__).resolve().parent
|
|
manifest=json.loads((ROOT/'content/image-manifest.json').read_text(encoding='utf-8'))
|
|
originals=ROOT/'artwork';originals.mkdir(exist_ok=True)
|
|
thumbs=[]
|
|
for item in manifest:
|
|
src=ROOT/item['path'];slug=item['slug']
|
|
dest=originals/(slug+'.png')
|
|
if src.resolve()!=dest.resolve():shutil.copy2(src,dest)
|
|
im=Image.open(src).convert('RGB')
|
|
assert im.size==(1536,1024),(slug,im.size)
|
|
for width in (480,960,1536):
|
|
export=im.resize((width,round(width*2/3)),Image.Resampling.LANCZOS)
|
|
export.save(ROOT/'assets'/f'{slug}-{width}.webp',quality=82,method=6)
|
|
tile=Image.new('RGB',(384,286),'#05070f');tile.paste(im.resize((384,256)),(0,0));ImageDraw.Draw(tile).text((12,265),slug,fill='#67e8f9');thumbs.append(tile)
|
|
sheet=Image.new('RGB',(384*3,286*((len(thumbs)+2)//3)),'#05070f')
|
|
for i,t in enumerate(thumbs):sheet.paste(t,((i%3)*384,(i//3)*286))
|
|
sheet.save(ROOT/'artwork/contact-sheet.jpg',quality=90)
|
|
print('Prepared',len(manifest),'illustrations; 3 sizes each')
|