Advertisement
here2share

# Tk_cave_gif.py

Jun 13th, 2021
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. # Tk_cave_gif.py
  2.  
  3. from Tkinter import *
  4. import random
  5. from PIL import Image, ImageTk
  6.  
  7.  
  8. size = 300
  9. light = 255
  10. dark = 0
  11. colorBalance = 0.5
  12. area = [-1,0,1]
  13.  
  14. root = Tk()
  15. root.title("Tk cave_gif")
  16. xm=ym=size
  17.  
  18. canvas = Canvas(root, width=xm, height=ym)
  19. canvas.grid()
  20.  
  21. screenShotsAtIterations = [0] + [2**i for i in range(10,21)]
  22. iterations = screenShotsAtIterations[-1]
  23. neighbours = [(i,j) for i in area for j in area if i!=0 or j!=0]
  24. neighboursCount = len(neighbours)
  25. rnd = random.Random()
  26. rnd.seed(0)
  27. image = Image.new('L',(size,size), light)
  28. pixel = image.load()
  29.  
  30. for x in range(size):
  31.     for y in range(size):
  32.         pixel[x,y] = rnd.random() < colorBalance and light or dark
  33.  
  34. c = 1
  35. for i in range(iterations+1):
  36.     if (i in screenShotsAtIterations):
  37.         filename = 'cave_' + str(c) + '.png'
  38.         c += 1
  39.         #image.save(filename)
  40.         print filename
  41.         '''
  42.         if i == screenShotsAtIterations[-1]:
  43.             image.resize((size*4,size*4)).save('cave_final_big.png')
  44.         '''
  45.  
  46.         imgTk = ImageTk.PhotoImage(image)
  47.         canvas.create_image((0,0),image=imgTk,anchor=NW)
  48.         root.update()
  49.  
  50.     x = rnd.randrange(size)
  51.     y = rnd.randrange(size)
  52.     currentColor = pixel[x,y]
  53.     darkNeighbours = sum(pixel[(x+i)%size,(y+j)%size] == dark for (i,j) in neighbours)
  54.     lightNeighbours = neighboursCount - darkNeighbours
  55.     if darkNeighbours > lightNeighbours:
  56.         pixel[x,y] = dark
  57.     elif lightNeighbours > darkNeighbours:
  58.         pixel[x,y] = light
  59.  
  60. # the code above can generate a set of still images,
  61. # which can be combined to an animated gif using this imagemagick command line:
  62. # convert cave_1*.png -set delay 15 -reverse cave_1*.png -set delay 25 -loop 0 cave.gif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement