Skip to content

Instantly share code, notes, and snippets.

@paiv
Last active November 30, 2017 15:42
Show Gist options
  • Save paiv/1e01aca5be1842fa09da5bb5f3934551 to your computer and use it in GitHub Desktop.
Save paiv/1e01aca5be1842fa09da5bb5f3934551 to your computer and use it in GitHub Desktop.
Conway's Game of Life
#!/usr/bin/env python3
import numpy as np
from PIL import Image
def conway():
size = (421, 1263)
iterations = 19780
data = np.random.randint(0, 2, size, dtype=np.uint8)
for i in range(0, iterations):
N = (data[0:-2,0:-2] + data[0:-2,1:-1] + data[0:-2,2:] +
data[1:-1,0:-2] + data[1:-1,2:] +
data[2: ,0:-2] + data[2: ,1:-1] + data[2: ,2:])
birth = (N == 3) & (data[1:-1,1:-1] == 0)
survive = ((N == 2) | (N == 3)) & (data[1:-1,1:-1] == 1)
data[...] = 0
data[1:-1,1:-1][birth | survive] = 1
data = np.ones(size, dtype=np.uint8) - data
data *= 255
image = Image.fromarray(data, mode='L')
image.save('image-{}.png'.format(iterations))
if __name__ == '__main__':
conway()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment