life_01.html
001|
002| from sys import argv
003| from os import path
004| from random import random
005| from PIL import Image, ImageDraw
006|
007| BasePath = r'C:\CJS\prj\Python\blog\proj'
008| ImgsPath = path.join(BasePath, 'img')
009| AniPath = path.join(BasePath, 'ani', '1')
010|
011|
012| CellSize = (4, 4)
013| LiveColor = (0, 0, 0)
014| DeadColor = (0xcf,0xdf,0xff)
015|
016|
017| Cols = 1280//4
018| Rows = 720//4
019|
020| Buffer1 = [[0 for c in range(Cols)] for r in range(Rows)]
021| Buffer2 = [[0 for c in range(Cols)] for r in range(Rows)]
022|
023|
024| def initialize_buffer (percentage=0.5):
025| global Buffer1, Buffer2
026| Buffer1 = [[0 for c in range(Cols)] for r in range(Rows)]
027| Buffer2 = [[0 for c in range(Cols)] for r in range(Rows)]
028| for r in range(Rows):
029| for c in range(Cols):
030| Buffer1[r][c] = (1 if random() < percentage else 0)
031|
032|
033| def print_buffers (fp, both=False):
034| out = []
035| for r in range(Rows):
036| row = [('*' if Buffer1[r][c] else '-') for c in range(Cols)]
037| out.append(''.join(row))
038| txt = '\n'.join(out)
039| print(txt, file=fp)
040| print(file=fp)
041| if both:
042| out = []
043| for r in range(Rows):
044| row = ['%d' % Buffer2[r][c] for c in range(Cols)]
045| out.append(''.join(row))
046| txt = '\n'.join(out)
047| print(txt, file=fp)
048| print(file=fp)
049|
050|
051| def create_image_file (fname, fpath=ImgsPath, mode='PNG'):
052| filename = path.join(fpath, fname)
053| dims = (Cols*CellSize[0], Rows*CellSize[1])
054| print('dims=(%d, %d)' % dims)
055| im = Image.new('RGB', dims, (255,255,255))
056| draw = ImageDraw.Draw(im)
057| for y in range(Rows):
058| for x in range(Cols):
059| v = Buffer1[y][x]
060| cc = LiveColor if 0<v else DeadColor
061| x0 = x * CellSize[0]
062| y0 = y * CellSize[1]
063| for yy in range(CellSize[1]):
064| for xx in range(CellSize[0]):
065| draw.point((x0+xx,y0+yy), fill=cc)
066| im.save(filename, mode)
067| print('wrote: %s' % filename)
068|
069|
070| def count_neighbors (row, col):
071|
072| total = 0
073|
074| if 0 < row:
075| total += (Buffer1[row-1][col-1] if 0 < col else 0)
076| total += (Buffer1[row-1][col])
077| total += (Buffer1[row-1][col+1] if col < (Cols-1) else 0)
078|
079| total += (Buffer1[row][col-1] if 0 < col else 0)
080| total += (Buffer1[row][col+1] if col < (Cols-1) else 0)
081|
082| if row < (Rows-1):
083| total += (Buffer1[row+1][col-1] if 0 < col else 0)
084| total += (Buffer1[row+1][col])
085| total += (Buffer1[row+1][col+1] if col < (Cols-1) else 0)
086|
087| Buffer2[row][col] = total
088| return total
089|
090|
091| def apply_rule (row, col):
092| m = Buffer1[row][col]
093| n = Buffer2[row][col]
094| if 0 < m:
095| return (1 if (n in [2,3]) else 0)
096| return (1 if n == 3 else 0)
097|
098|
099| def life_cycle ():
100|
101| for r in range(Rows):
102| for c in range(Cols):
103| count_neighbors(r,c)
104|
105| for r in range(Rows):
106| for c in range(Cols):
107| Buffer1[r][c] = apply_rule(r,c)
108|
109|
110| def do_life (cycles=20, percentage=0.5):
111| initialize_buffer(percentage)
112| for n in range(cycles):
113| life_cycle()
114| fn = 'life-%05d.png' % n
115| create_image_file(fname=fn, fpath=AniPath)
116|
117|
118| def main ():
119| do_life(1600)
120|
121|
122| if __name__ == '__main__':
123| print(argv[0])
124| print()
125| main()
126| print()
127|
128| '''eof'''
129|