Skip to content

Instantly share code, notes, and snippets.

@EllieTheYeen
Created December 8, 2020 01:49
Show Gist options
  • Save EllieTheYeen/43706fda3f3a3155755883aa53fa57a0 to your computer and use it in GitHub Desktop.
Save EllieTheYeen/43706fda3f3a3155755883aa53fa57a0 to your computer and use it in GitHub Desktop.
import os
from PIL import Image
import numpy as np
width, height = 800, 600
grid = np.zeros((height, width), dtype=np.uint8)
x = y = int(width / 2)
d = 0
imgs = []
for i in range(10):
for a in range(100000):
if grid[y, x]:
d -= 1
else:
d += 1
d %= 4
#if d < 0: d = 3
#elif d > 3: d = 0
grid[y, x] = 1 - grid[y, x]
#print(grid[x, y])
if d == 0: y -= 1
elif d == 1: x += 1
elif d == 2: y += 1
else: x -= 1
x %= width
y %= height
#if x >= width: x = 0
#elif x < 0: x = width - 1
#elif y >= height: y = 0
#elif y < 0: y = height - 1
#print(x, y, d)
ia = ((1 - grid) * 255).repeat(3).reshape(height, width, 3)
i = Image.fromarray(ia)
imgs.append(i)
os.chdir(os.path.split(__file__)[0] or '.')
print('Sum is', grid.sum())
imgs[0].save('output.gif', loop=0, save_all=True, append_images=imgs[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment