HarvestForTwo / tools /render_partner_faces.py
princessamygdala's picture
Claude Opus 5
The partner questions stay at the end, and every partner has a face
f52b886
Raw History Blame Contribute Delete
2.64 kB
"""Render static/partner_face_<idx>.png from the game's own avatar code.
The OTHER task's belief and liking pages name this task's partners and show
their faces. It cannot draw them: its art is explorers, and a farmer redrawn
as an explorer is a different-looking person from the one the participant
met. So each Space serves pictures of its own partners at
order_link.PARTNER_FACE_PATH and the other one links to them.
The art lives in static/cornfield.js and draws to a canvas, so the pictures
come out of a real browser rather than a PIL reimplementation, which would
drift from what the matching screen shows the moment either copy changed.
How they were made, and how to remake them when the palettes change:
1. Run the app locally.
RELOAD=0 STORAGE_SECRET=test PORT=8099 python web_app.py
2. Open any page of it, so cornfield.js is loaded, and evaluate RENDER_JS
below. 72x88 is the size the matching reveal draws at, and
partnerAvatarPreview() has a fixed internal scale, so a bigger canvas
gives a small figure in a large field of soil rather than a bigger
figure.
3. Save each base64 body to static/partner_face_<i>.png.
4. Run this file, which doubles them with nearest-neighbour. The art is
flat-coloured, so doubling keeps the edges hard and the picture stays
sharp on a retina screen.
python tools/render_partner_faces.py
"""
import pathlib
from PIL import Image
ROOT = pathlib.Path(__file__).resolve().parents[1]
OUT = ROOT / "static"
#: Evaluated in a page that has already loaded cornfield.js. Returns a list of
#: base64 PNG bodies, one per partner palette.
RENDER_JS = """
(function () {
const n = window.Cornfield.numPartnerAvatars();
const out = [];
for (let i = 0; i < n; i++) {
const cv = document.createElement("canvas");
cv.width = 72; cv.height = 88; cv.id = "__pf_" + i;
document.body.appendChild(cv);
window.Cornfield.partnerAvatarPreview(cv.id, i);
out.push(cv.toDataURL("image/png").split(",")[1]);
cv.remove();
}
return out;
})()
"""
def double(paths=None):
"""Nearest-neighbour 2x, in place. Skips anything already doubled."""
done = []
for path in sorted(paths or OUT.glob("partner_face_*.png")):
im = Image.open(path)
if im.width >= 144:
continue
im.resize((im.width * 2, im.height * 2), Image.NEAREST).save(path)
done.append(path.name)
return done
if __name__ == "__main__":
print(__doc__)
print("doubled:", double() or "(nothing to do)")
for p in sorted(OUT.glob("partner_face_*.png")):
print(f" {p.name} {Image.open(p).size}")