life_02.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', '2')
010|
011|
012| CellSize = (4, 4)
013| LiveColor = (0xdf,0xef,0xff)
014| DeadColor = (0,0,0)
015|
016|
017| Cols = 1280//CellSize[0]
018| Rows = 720//CellSize[1]
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| left = (Cols-1) if col == 0 else (col-1)
074| right = (col+1) if col < (Cols-1) else 0
075| top = (Rows-1) if row == 0 else (row-1)
076| bot = (row+1) if row < (Rows-1) else 0
077|
078| total += Buffer1[top][left]
079| total += Buffer1[top][col]
080| total += Buffer1[top][right]
081|
082| total += Buffer1[row][left]
083| total += Buffer1[row][right]
084|
085| total += Buffer1[bot][left]
086| total += Buffer1[bot][col]
087| total += Buffer1[bot][right]
088|
089| Buffer2[row][col] = total
090| return total
091|
092|
093| def apply_rule (row, col):
094| m = Buffer1[row][col]
095| n = Buffer2[row][col]
096| if 0 < m:
097| return (1 if (n in [2,3]) else 0)
098| return (1 if n == 3 else 0)
099|
100|
101| def life_cycle ():
102|
103| for r in range(Rows):
104| for c in range(Cols):
105| count_neighbors(r,c)
106|
107| for r in range(Rows):
108| for c in range(Cols):
109| Buffer1[r][c] = apply_rule(r,c)
110|
111|
112| def do_life (cycles=20, percentage=0.5):
113| initialize_buffer(percentage)
114| for n in range(cycles):
115| fn = 'life-%05d.png' % n
116| create_image_file(fname=fn, fpath=AniPath)
117| life_cycle()
118|
119|
120| def main ():
121| do_life(cycles=6000, percentage=0.15)
122|
123|
124| if __name__ == '__main__':
125| print(argv[0])
126| print()
127| main()
128| print()
129|
130| '''eof'''
131|