Advertisement
here2share

# tk_evolving_semi_blotches.py

Jan 17th, 2024
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. # tk_evolving_semi_blotches.py
  2.  
  3. from tkinter import Tk, Canvas
  4. from PIL import Image, ImageTk
  5. import random
  6.  
  7. ww, hh = 600, 600
  8. root = Tk()
  9. root.title("# tk_evolving_semi_blotches.py")
  10. ww0, hh0 = ww // 3, hh // 3
  11. canvas = Canvas(root, width=ww, height=hh)
  12. canvas.pack()
  13.  
  14. while True:
  15.     feed = 0.000001
  16.  
  17.     grid = [[random.randint(0, 255) for _ in range(ww0)] for _ in range(hh0)]
  18.     new_grid = [[0 for _ in range(ww0)] for _ in range(hh0)]
  19.  
  20.     stop = 0
  21.     while True:
  22.         pil_img = Image.new('L', (ww0, hh0), 'white')
  23.         pixels = pil_img.load()
  24.         for i in range(1, hh0 - 1):
  25.             for j in range(1, ww0 - 1):
  26.                 total = -(grid[i-1][j-1] + grid[i-1][j] + grid[i-1][j+1] + grid[i][j-1] + grid[i][j+1] + grid[i+1][j-1] + grid[i+1][j] + grid[i+1][j+1] + feed) / 50
  27.                 new_grid[i][j] = grid[i][j] + total
  28.                 try:
  29.                     pixels[i, j] = int(new_grid[i][j])
  30.                 except:
  31.                     stop = 1
  32.         if stop:
  33.             break
  34.         else:
  35.             grid = new_grid[:]
  36.             pil_img = pil_img.resize((ww, hh), Image.BICUBIC)
  37.             tk_img = ImageTk.PhotoImage(pil_img)
  38.             canvas.create_image(0, 0, anchor='nw', image=tk_img)
  39.             canvas.update()
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement