Skip to content

Instantly share code, notes, and snippets.

@Axeltherabbit
Created February 9, 2022 15:36
Show Gist options
  • Save Axeltherabbit/eeaba8c2f29b14ec67775007b7f64219 to your computer and use it in GitHub Desktop.
Save Axeltherabbit/eeaba8c2f29b14ec67775007b7f64219 to your computer and use it in GitHub Desktop.
from PIL import Image
# 1 is white
# 0 is black
m = [
[0],
]
print("generating")
for _ in range(10000):
newl = [1]
for i in range(len(m[-1])):
match [
*(m[-1][i - 1 : i] or [1]),
m[-1][i],
*(m[-1][i + 1 : i + 2] or [1]),
]: # this monster is a poor attempt to avoid the creation of a temporary list 1+m[-1]+1
case [0, 0, 0] | [0, 0, 1] | [0, 1, 0] | [1, 1, 1]:
newl.append(1)
case [0, 1, 1] | [1, 0, 0] | [1, 0, 1] | [1, 1, 0]:
newl.append(0)
newl.append(1)
m.append(newl)
size = len(m[-1]), len(m) # width, height
mode = "1" # 1 bit pixels, black or white
image_out = Image.new(mode, size)
print("extending list")
for l in m:
lenght = (len(m[-1]) - len(l)) // 2
l[0:0] = [1] * lenght # extendleft
l[len(l) : -1] = [1] * lenght # extendright
print("flattening and generating image")
image_out.putdata([e for l in m for e in l])
image_out.save("test_out.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment