TheBird

rect.py

2.3 kB · python · 89 lines

1import mrlypy.two as m22import mrlypy.three as m33import mrlypy.six as m64from mrlypy.core.colors import alpha, black, blue, green, red, white5from config import DATA_DIR6from PIL import Image, ImageDraw7from typing import List, Tuple89# HELPERS10def resize(image: Image.Image, orientation: str) -> Image.Image:11    import math12    ratio = math.sqrt(3) / 213    w, h = image.size14    if orientation == "horizontal":15        w = image.width16        h = int(image.height * ratio)17    if orientation == "vertical":18        w = int(image.width * ratio)19        h = image.height20    return image.resize((w, h), Image.Resampling.NEAREST)2122def get_parity(value: int) -> str:23    return "EVEN" if value % 2 == 0 else "ODD"2425def get_start(name: str) -> int:26    value = 027    if "iso" in name:28        value = 129    if "pro" in name:30        value = 131    if "cut" in name:32        value = 033    if get_parity(NUMBER**LEVEL) == "EVEN":34        value = not value35    return not value if FLIP else value3637def save_draw(cell: m2.Cell2d, name: str):38    fp = f"{DATA_DIR}/rect_{name}.png"39    image = m6.rect_draw(cell, 10, get_start(name))40    if RESIZE:41        image = resize(image, m6.get_orientation(cell.width, cell.height))42    image.save(fp)43    print(f"Saved: {fp}")44    if TILE:45        fp = f"{DATA_DIR}/rect_{name}_{WIDTH}_{HEIGHT}.png"46        image = m2.Cell2d.from_image(image).tile(WIDTH, HEIGHT).to_image(scale=1)47        image.save(fp)48        print(f"Saved: {fp}")49    return cell5051def save_svg(cell: m2.Cell2d, name: str):52    fp = f"{DATA_DIR}/rect_{name}.svg"53    svg = m6.rect_svg(cell, 10, get_start(name))54    with open(fp, "w") as f:55        f.write(svg)56    print(f"Saved: {fp}")5758def print_cell(cell: m2.Cell2d, name: str):59    print(name)60    for row in cell.text():61        print(row)62    print()6364def save(cell: m2.Cell2d, name: str):65    save_draw(cell, name)66    save_svg(cell, name)6768def new_cell():69    return m3.carpet_3d(NUMBER, LEVEL)7071# SETTINGS7273SCALE = 1074WIDTH = 375HEIGHT = 376TILE = True77RESIZE = True78NUMBER = 579LEVEL = 180FLIP = False8182def main():83    palette = {0: [white], 1: [black], 2: [alpha], 3: [red], 4: [green], 5: [blue]}84    save(m6.iso(new_cell()).paint(palette), "iso")85    save(m6.pro(new_cell()).paint(palette), "pro")86    save(m6.cut(new_cell()).paint(palette), "cut")8788if __name__ == "__main__":89    main()