Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_Ultra_Fast_Reaction_Diffusion.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageFilter
- import random
- ww = 560
- hh = 560
- ww0, hh0 = ww + 2, hh + 2
- center_x = ww // 2
- center_y = hh // 2
- root = tk.Tk()
- root.title("tk_Ultra_Fast_Reaction_Diffusion")
- root.geometry('+0+0')
- canvas = tk.Canvas(root, width=ww, height=hh, bg='white')
- def update_params_x(value):
- global x0
- x0 = float(value)
- def update_params_y(value):
- global y0
- y0 = float(value)
- def update_params_z(value):
- global z0
- z0 = float(value)
- x0 = 5
- y0 = 5
- z0 = 2
- even_dist = [192] * (ww0*hh0//2)
- even_dist += [80] * (ww0*hh0//2)
- random.shuffle(even_dist)
- x_slider = tk.Scale(root, from_=10, to=0, label='X', resolution=1, orient=tk.VERTICAL, bg="#FFFF00")
- x_slider.set(x0)
- y_slider = tk.Scale(root, from_=10, to=0, label='Y', resolution=1, orient=tk.VERTICAL, bg="#00FF00")
- y_slider.set(y0)
- z_slider = tk.Scale(root, from_=10, to=0, label='Z', resolution=1, orient=tk.VERTICAL, bg="#00C0FF")
- z_slider.set(z0)
- canvas.pack(side=tk.LEFT)
- x_slider.pack(side=tk.LEFT, fill='y')
- y_slider.pack(side=tk.LEFT, fill='y')
- z_slider.pack(side=tk.LEFT, fill='y')
- x_slider.config(command=lambda value: update_params_x(x_slider.get()))
- y_slider.config(command=lambda value: update_params_y(y_slider.get()))
- z_slider.config(command=lambda value: update_params_z(z_slider.get()))
- ref_image = Image.new("RGB", (ww0, hh0), "white")
- kernel_values = [0.05, 0.2, 0.05, 0.2, -1, 0.2, 0.05, 0.2, 0.05]
- kernel = ImageFilter.Kernel((3, 3), kernel_values, scale=None, offset=0)
- def display():
- img = ref_image.filter(kernel)
- img = img.crop((0, 0, ww, hh))
- img_tk = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor="nw", image=img_tk)
- canvas.img = img_tk
- root.update()
- ref_image.putpixel((center_x, center_y - 1), (255, 255, 255))
- ref_image.putpixel((center_x, center_y + 1), (0, 0, 0))
- pixel_data = []
- for i in range(hh0):
- for j in range(ww0):
- c = even_dist.pop()
- pixel_data.append((c, c, c))
- ref_image.putdata(pixel_data)
- display()
- while True:
- x, y, z = x0, y0, z0
- speckle = int(max(0, x0-10) + max(0, y0-10) + max(0, z0-10))
- ref_image = ref_image.filter(ImageFilter.GaussianBlur(x))
- ref_image = ref_image.filter(kernel)
- ref_image = ref_image.filter(ImageFilter.GaussianBlur(y))
- ref_image = ref_image.filter(kernel)
- ref_image = ref_image.filter(ImageFilter.GaussianBlur(z))
- display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement