Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # turing_animation.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageFilter, ImageEnhance
- import random
- import math
- size = 600
- def create_turing_pattern():
- chemical_a = Image.new("L", (size, size))
- chemical_a.putdata([int(random.random() * 255) for _ in range(size * size)])
- kernel = ImageFilter.Kernel(
- size=(3, 3),
- kernel=[
- 0.05, 0.2, 0.05,
- 0.2, -1.0, 0.2,
- 0.05, 0.2, 0.05
- ],
- scale=None,
- offset=0
- )
- wave = 0
- while True:
- wave = (wave + 45) % 360
- wave_value = control_b * math.sin(math.radians(wave))
- laplacian = chemical_a.filter(kernel)
- laplacian = laplacian.filter(ImageFilter.GaussianBlur(radius=control_a + wave_value))
- laplacian = ImageEnhance.Contrast(laplacian).enhance(2.0)
- data = laplacian.getdata()
- chemical_a.putdata(data)
- if wave % 2:
- img.putdata(data)
- canvas_image = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=tk.NW, image=canvas_image)
- root.update_idletasks()
- root.update()
- def update_control_a(e=None):
- global control_a
- control_a = control_a_slider.get()
- def update_control_b(e=None):
- global control_b
- control_b = control_b_slider.get()
- def update_control_sharpen(e=None):
- global control_sharpen
- control_sharpen = control_sharpen_slider.get()
- root = tk.Tk()
- canvas = tk.Canvas(root, width=size+250, height=size)
- canvas.pack()
- img = Image.new("RGB", (size, size))
- L = 560
- control_a_slider = tk.Scale(root, from_=0.001, to=5, orient=tk.VERTICAL, resolution=0.0001, length=L, command=update_control_a)
- control_a_slider.set(2) # Set default value
- control_a_slider.place(x=size+70, y=20, anchor=tk.NE)
- control_b_slider = tk.Scale(root, from_=0.001, to=5, orient=tk.VERTICAL, resolution=0.0001, length=L, command=update_control_b)
- control_b_slider.set(0.5) # Set default value
- control_b_slider.place(x=size+140, y=20, anchor=tk.NE)
- control_sharpen_slider = tk.Scale(root, from_=0, to=2, orient=tk.VERTICAL, resolution=0.001, length=L, command=update_control_sharpen)
- control_sharpen_slider.set(1) # Set default value
- control_sharpen_slider.place(x=size+210, y=20, anchor=tk.NE)
- update_control_a()
- update_control_b()
- update_control_sharpen()
- create_turing_pattern()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement