Files
site-servaki-online/prepare_photos.py
T

25 lines
1.3 KiB
Python

"""Preserve generated editorial photographs and export responsive WebP versions."""
from pathlib import Path
from PIL import Image, ImageDraw, ImageOps
import json, shutil
ROOT=Path(__file__).resolve().parent
manifest=json.loads((ROOT/'content/photo-manifest.json').read_text(encoding='utf-8'))
originals=ROOT/'artwork/photos-selected'; originals.mkdir(parents=True,exist_ok=True)
tiles=[]
for item in manifest:
slug=item['slug']; source=ROOT/item['path']
dest=originals/(slug+'.png')
if source.resolve()!=dest.resolve():shutil.copy2(source,dest)
picture=Image.open(source).convert('RGB')
picture=ImageOps.fit(picture,(1536,1024),method=Image.Resampling.LANCZOS,centering=(.5,.5))
for width in (480,960,1536):
picture.resize((width,round(width*2/3)),Image.Resampling.LANCZOS).save(ROOT/'assets'/f'photo-{slug}-{width}.webp',quality=83,method=6)
tile=Image.new('RGB',(384,280),'#05070f')
tile.paste(picture.resize((384,256)),(0,0))
ImageDraw.Draw(tile).text((12,263),slug,fill='#67e8f9')
tiles.append(tile)
sheet=Image.new('RGB',(1152,280*((len(tiles)+2)//3)),'#05070f')
for index,tile in enumerate(tiles):sheet.paste(tile,((index%3)*384,(index//3)*280))
sheet.save(originals/'contact-sheet.jpg',quality=88)
print('Prepared',len(manifest),'warm editorial photographs in 3 responsive sizes')