Advertisement
here2share

# tk_Ultra_Fast_Reaction_Diffusion.py

Jan 7th, 2024 (edited)
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. # tk_Ultra_Fast_Reaction_Diffusion.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageFilter
  5. import random
  6.  
  7. ww = 560
  8. hh = 560
  9. ww0, hh0 = ww + 2, hh + 2
  10. center_x = ww // 2
  11. center_y = hh // 2
  12.  
  13. root = tk.Tk()
  14. root.title("tk_Ultra_Fast_Reaction_Diffusion")
  15. root.geometry('+0+0')
  16.  
  17. canvas = tk.Canvas(root, width=ww, height=hh, bg='white')
  18.  
  19. def update_params_x(value):
  20.     global x0
  21.     x0 = float(value)
  22.  
  23. def update_params_y(value):
  24.     global y0
  25.     y0 = float(value)
  26.  
  27. def update_params_z(value):
  28.     global z0
  29.     z0 = float(value)
  30.    
  31. x0 = 5
  32. y0 = 5
  33. z0 = 2
  34.  
  35. even_dist = [192] * (ww0*hh0//2)
  36. even_dist += [80] * (ww0*hh0//2)
  37. random.shuffle(even_dist)
  38.  
  39. x_slider = tk.Scale(root, from_=10, to=0, label='X', resolution=1, orient=tk.VERTICAL, bg="#FFFF00")
  40. x_slider.set(x0)
  41.  
  42. y_slider = tk.Scale(root, from_=10, to=0, label='Y', resolution=1, orient=tk.VERTICAL, bg="#00FF00")
  43. y_slider.set(y0)
  44.  
  45. z_slider = tk.Scale(root, from_=10, to=0, label='Z', resolution=1, orient=tk.VERTICAL, bg="#00C0FF")
  46. z_slider.set(z0)
  47.  
  48. canvas.pack(side=tk.LEFT)
  49. x_slider.pack(side=tk.LEFT, fill='y')
  50. y_slider.pack(side=tk.LEFT, fill='y')
  51. z_slider.pack(side=tk.LEFT, fill='y')
  52.  
  53. x_slider.config(command=lambda value: update_params_x(x_slider.get()))
  54. y_slider.config(command=lambda value: update_params_y(y_slider.get()))
  55. z_slider.config(command=lambda value: update_params_z(z_slider.get()))
  56.  
  57. ref_image = Image.new("RGB", (ww0, hh0), "white")
  58.  
  59. kernel_values = [0.05, 0.2, 0.05, 0.2, -1, 0.2, 0.05, 0.2, 0.05]
  60. kernel = ImageFilter.Kernel((3, 3), kernel_values, scale=None, offset=0)
  61. def display():
  62.     img = ref_image.filter(kernel)
  63.     img = img.crop((0, 0, ww, hh))
  64.     img_tk = ImageTk.PhotoImage(img)
  65.     canvas.create_image(0, 0, anchor="nw", image=img_tk)
  66.     canvas.img = img_tk
  67.     root.update()
  68.     ref_image.putpixel((center_x, center_y - 1), (255, 255, 255))
  69.     ref_image.putpixel((center_x, center_y + 1), (0, 0, 0))
  70.  
  71. pixel_data = []
  72. for i in range(hh0):
  73.     for j in range(ww0):
  74.         c = even_dist.pop()
  75.         pixel_data.append((c, c, c))
  76. ref_image.putdata(pixel_data)
  77. display()
  78.  
  79. while True:
  80.     x, y, z = x0, y0, z0
  81.     speckle = int(max(0, x0-10) + max(0, y0-10) + max(0, z0-10))
  82.     ref_image = ref_image.filter(ImageFilter.GaussianBlur(x))
  83.     ref_image = ref_image.filter(kernel)
  84.     ref_image = ref_image.filter(ImageFilter.GaussianBlur(y))
  85.     ref_image = ref_image.filter(kernel)
  86.     ref_image = ref_image.filter(ImageFilter.GaussianBlur(z))
  87.     display()
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement