1+ import numpy as np
2+ from imagepy import IPy
3+ from imagepy .core import ImagePlus
4+ from imagepy .core .engine import Free , Tool
5+ from scipy .ndimage import label
6+ from scipy .signal import convolve2d
7+
8+ def generate (scr , size ):
9+ row , col = scr .shape
10+ arr = np .ones ((row * (size )+ 1 , col * (size )+ 1 ), dtype = np .uint8 )
11+ xs , ys = np .where (arr [:- 1 ,:- 1 ])
12+ arr [xs , ys ] = scr [xs // size , ys // size ]
13+ arr [::size ,:] = 2 ; arr [:,::size ] = 2
14+ return np .array ([128 ,255 ,0 ], dtype = np .uint8 )[arr ]
15+
16+ def getscr (arr , size ): return arr [1 ::size , 1 ::size ]// 255
17+
18+ def run (scr ):
19+ pad = np .pad (scr , 1 , 'constant' )
20+ core = np .array ([[1 ,1 ,1 ],[1 ,9 ,1 ],[1 ,1 ,1 ]])
21+ cov = convolve2d (pad , core , 'valid' )
22+ lut = [0 ,0 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,0 ,0 ,0 ,0 ,0 ]
23+ return np .array (lut , dtype = np .uint8 )[cov .astype (np .uint8 )]
24+
25+ class Painter (Tool ):
26+ def __init__ (self , size ): self .size = size
27+ title = 'Life Painter'
28+
29+ def mouse_down (self , ips , x , y , btn , ** key ):
30+ if btn == 1 :
31+ r , c = int (y ), int (x )
32+ if r < 0 or r > ips .img .shape [0 ]: return
33+ if c < 0 or c > ips .img .shape [1 ]: return
34+ if ips .img [r ,c ] == 0 : return
35+ lab , n = label (ips .img > 0 )
36+ ips .img [lab == lab [r ,c ]] = (128 ,255 )[ips .img [r ,c ]!= 255 ]
37+ ips .update ()
38+ if btn == 3 :
39+ img = getscr (ips .img , self .size )
40+ for i in range (len (ips .imgs )):
41+ ips .imgs [i ][:] = generate (img , self .size )
42+ img = run (img )
43+ IPy .alert ('Complete!' )
44+
45+ class Plugin (Free ):
46+ title = 'Game Of Life'
47+ para = {'name' :'Game01' ,'width' :15 , 'height' :15 , 'size' :30 ,'slice' :30 }
48+ view = [(str , 'name' , 'name' , '' ),
49+ (int , 'width' , (1 ,2048 ), 0 , 'width' , 'pix' ),
50+ (int , 'height' , (1 ,2048 ), 0 , 'height' , 'pix' ),
51+ (int , 'size' , (10 , 50 ), 0 , 'size' , '' ),
52+ (int , 'slice' , (1 ,100 ), 0 , 'slice' , '' )]
53+
54+ #process
55+ def run (self , para = None ):
56+ first = generate (np .zeros ((para ['height' ], para ['width' ])), para ['size' ])
57+ imgs = [first .copy () for i in range (para ['slice' ])]
58+ ips = ImagePlus (imgs , para ['name' ])
59+ ips .tool = Painter (para ['size' ])
60+ IPy .show_ips (ips )
0 commit comments