Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_erosion.py
- from tkinter import *
- import time
- from PIL import Image, ImageTk
- from random import shuffle as rs, randint as ri
- tx = time.time
- ww = 180
- hh = 180
- root = Tk()
- root.title("Tk_erosion")
- root.geometry("%dx%d+-6+-2"%(ww,hh))
- canvas = Canvas(width=ww, height=hh)
- canvas.pack()
- def rgb2hex(r,g,b):
- return '#%02X%02X%02X'%(r,g,b)
- def motion(event):
- mouse_pos.append((event.x, event.y))
- root.bind('<B1-Motion>', motion)
- cols = ww
- rows = hh
- chemicals = []
- i = 0
- for z in range(ww*hh):
- chemicals += [(i+1)]
- i = (i+1)%25
- rs(chemicals)
- rgb = []
- xy = {}
- for j in range(0,rows):
- for i in range(0,cols):
- xy[i,j] = len(rgb)
- rgb += [(chemicals.pop())]
- current = rgb[:]
- previous = rgb[:]
- mouse_pos = []
- image = Image.new("RGB", (ww,hh))
- def draw():
- image.putdata(rgb)
- photo = ImageTk.PhotoImage(image)
- canvas.create_image(0,0,image=photo,anchor=NW)
- canvas.update()
- def sides(i,j):
- return ([i-1,j],[i+1,j],[i,j-1],[i,j+1])
- swap = 1
- #Mainloop
- while 1:
- for i in range(1,cols-1):
- for j in range(1,rows-1):
- if mouse_pos:
- x,y = mouse_pos.pop(0)
- for x2,y2 in sides(x,y):
- try:
- t = xy[x2,y2]
- chemicals += [current[t]]
- current[t] = 0
- except:
- 0
- t = xy[i,j]
- sides2 = (sum([current[xy[x,y]] for x,y in sides(i,j)])+current[t])/5
- if sides2 > 14: # the magic
- current[t], swap = swap, current[t]
- if current[t] == previous[t]:
- val = current[t]*10
- else:
- val = 255
- rgb[t] = (val,val,val)
- previous[t] = current[t]
- # print(tx)
- draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement