start.py
1.5 kB · python · 52 lines
1import math23import lib # noqa: F40145from PIL import Image, ImageDraw, ImageFont6from mrlypy.core.colors import alpha7from mrlypy.six import FILL, GRID, VOID8from mrlypy.six.designs import carpet_cut9from mrlypy.six.renderer import draw1011from lib.canvas import H3, INK, PAPER, flatten, quantize12from lib.gif import write_gif13from lib.paths import GIFS, ensure, show1415NUMBER = 516LEVEL = 217SUPER = 318GIFW = 81019GIFH = round(GIFW * H3)20GREYS = 1621PALETTE = {VOID: [PAPER], FILL: [INK], GRID: [alpha]}2223# FRAME2425def label(canvas, start):26 try:27 font = ImageFont.load_default(48)28 except TypeError:29 font = ImageFont.load_default()30 ImageDraw.Draw(canvas).text((30, GIFH - 78), f"start={start}", fill=INK.r, font=font)3132def frame(cell, start):33 scale = max(1, math.ceil(SUPER * GIFW / (cell.width + 1)))34 image = flatten(draw(cell, scale=scale, start=start)).convert("L")35 height = max(1, round(GIFW * image.height * H3 / image.width))36 image = image.resize((GIFW, height), Image.BOX)37 canvas = Image.new("L", (GIFW, GIFH), PAPER.r)38 canvas.paste(image, ((GIFW - image.width) // 2, (GIFH - image.height) // 2))39 label(canvas, start)40 return quantize(canvas, GREYS)4142# RUN4344def main():45 ensure()46 cell = carpet_cut(NUMBER, LEVEL).paint(PALETTE)47 path = GIFS / ("start-%d-%d.gif" % (NUMBER, LEVEL))48 write_gif(path, [frame(cell, start) for start in (0, 1)])49 print("%s (start 0, start 1)" % show(path))5051if __name__ == "__main__":52 main()