PCv5/assets/trophies-generate.py

36 lines
1018 B
Python
Executable File

#! /usr/bin/python3
from PIL import Image
import os
import sys
import slugify
import yaml
if os.path.basename(os.getcwd()) != "assets":
print("This script should be started from the /assets folder of PCv5.")
sys.exit(1)
# Read the list of trophies from /app/data/trophies.yaml
with open("../app/data/trophies.yaml") as fp:
trophies = yaml.safe_load(fp.read())
names = [ slugify.slugify(t["name"]) for t in trophies ]
# Write trophy images to /app/static/images/trophies
try:
os.mkdir("../app/static/images/trophies")
except FileExistsError:
pass
# Skip blank squares in the source image
img = Image.open("trophies.png")
def trophy_iterator(img):
for y in range(img.height // 26):
for x in range(img.width // 26):
icon = img.crop((26*x+1, 26*y+1, 26*x+25, 26*y+25))
if len(icon.getcolors()) > 1:
yield icon.resize((48,48))
for (name, icon) in zip(names, trophy_iterator(img)):
icon.save(f"../app/static/images/trophies/{name}.png")