TheBird

art.py

1.7 kB · python · 55 lines

1import numpy as np2from config import DATA_DIR3from helpers import is_prime4from PIL import Image, ImageDraw56def heatmap(size: int, limit: int):7    x, y = np.indices((size, size))8    x = x + 19    y = y + 110    u = x / size11    v = y / size12    heatmap = np.zeros((size, size), dtype=np.float32)13    for n in range(1, limit + 1, 2):14        parity_x = (np.floor(n * u) % 2 == 0).astype(float)15        parity_y = (np.floor(n * v) % 2 == 0).astype(float)16        heatmap += parity_x * parity_y17    heatmap = (heatmap - heatmap.min()) / (heatmap.max() - heatmap.min()) * 25518    img = Image.fromarray(heatmap.astype(np.uint8))19    fp = f"{DATA_DIR}/heatmap_size_{size}_limit_{limit}.png"20    img.save(fp)21    print(f"Saved: {fp}")2223def lines(size: int, limit: int):24    img = Image.new('RGB', (size, size), 'white')25    draw = ImageDraw.Draw(img)26    for n in range(2, limit + 1, 2):27        if is_prime(n):28            continue29        dots = []30        for k in range(0, n + 1):31            dots.append((k/n, 0))32            dots.append((k/n, 1))33        for k in range(1, n):34            dots.append((0, k/n))35            dots.append((1, k/n))36        dots = list(set(dots))37        for i in range(len(dots)):38            p1 = dots[i]39            x1, y1 = p1[0] * (size - 1), p1[1] * (size - 1)40            for j in range(i + 1, len(dots)):41                p2 = dots[j]42                x2, y2 = p2[0] * (size - 1), p2[1] * (size - 1)43                draw.line([x1, y1, x2, y2], fill=(0, 0, 0), width=1)44    fp = f"{DATA_DIR}/lines_size_{size}_limit_{limit}.png"45    img.save(fp)46    print(f"Saved: {fp}")4748def main():49    size = 100050    limit = 551    heatmap(size, limit)52    lines(size, limit)5354if __name__ == "__main__":55    main()