Advertisement
here2share

# turing_animation.py

Jun 11th, 2024
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. # turing_animation.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageFilter, ImageEnhance
  5. import random
  6. import math
  7.  
  8. size = 600
  9.  
  10. def create_turing_pattern():
  11.     chemical_a = Image.new("L", (size, size))
  12.     chemical_a.putdata([int(random.random() * 255) for _ in range(size * size)])
  13.  
  14.     kernel = ImageFilter.Kernel(
  15.         size=(3, 3),
  16.         kernel=[
  17.             0.05, 0.2, 0.05,
  18.             0.2, -1.0, 0.2,
  19.             0.05, 0.2, 0.05
  20.         ],
  21.         scale=None,
  22.         offset=0
  23.     )
  24.  
  25.     wave = 0
  26.     while True:
  27.         wave = (wave + 45) % 360
  28.         wave_value = control_b * math.sin(math.radians(wave))
  29.         laplacian = chemical_a.filter(kernel)
  30.         laplacian = laplacian.filter(ImageFilter.GaussianBlur(radius=control_a + wave_value))
  31.         laplacian = ImageEnhance.Contrast(laplacian).enhance(2.0)
  32.        
  33.         data = laplacian.getdata()
  34.        
  35.         chemical_a.putdata(data)
  36.        
  37.         if wave % 2:
  38.             img.putdata(data)
  39.             canvas_image = ImageTk.PhotoImage(img)
  40.             canvas.create_image(0, 0, anchor=tk.NW, image=canvas_image)
  41.             root.update_idletasks()
  42.             root.update()
  43.  
  44. def update_control_a(e=None):
  45.     global control_a
  46.     control_a = control_a_slider.get()
  47.  
  48. def update_control_b(e=None):
  49.     global control_b
  50.     control_b = control_b_slider.get()
  51.  
  52. def update_control_sharpen(e=None):
  53.     global control_sharpen
  54.     control_sharpen = control_sharpen_slider.get()
  55.  
  56. root = tk.Tk()
  57. canvas = tk.Canvas(root, width=size+250, height=size)
  58. canvas.pack()
  59.  
  60. img = Image.new("RGB", (size, size))
  61.  
  62. L = 560
  63. control_a_slider = tk.Scale(root, from_=0.001, to=5, orient=tk.VERTICAL, resolution=0.0001, length=L, command=update_control_a)
  64. control_a_slider.set(2)  # Set default value
  65. control_a_slider.place(x=size+70, y=20, anchor=tk.NE)
  66.  
  67. control_b_slider = tk.Scale(root, from_=0.001, to=5, orient=tk.VERTICAL, resolution=0.0001, length=L, command=update_control_b)
  68. control_b_slider.set(0.5)  # Set default value
  69. control_b_slider.place(x=size+140, y=20, anchor=tk.NE)
  70.  
  71. control_sharpen_slider = tk.Scale(root, from_=0, to=2, orient=tk.VERTICAL, resolution=0.001, length=L, command=update_control_sharpen)
  72. control_sharpen_slider.set(1)  # Set default value
  73. control_sharpen_slider.place(x=size+210, y=20, anchor=tk.NE)
  74.  
  75. update_control_a()
  76. update_control_b()
  77. update_control_sharpen()
  78.  
  79. create_turing_pattern()
  80. root.mainloop()
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement