Advertisement
here2share

# tk_Reaction_Diffusion_RGB_Zoom.py

Jan 8th, 2024 (edited)
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. # tk_Reaction_Diffusion_RGB_Zoom.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_Reaction_Diffusion_RGB_Zoom")
  15. root.geometry('+0+0')
  16.  
  17. canvas = tk.Canvas(root, width=ww, height=hh, bg='white')
  18.    
  19. ref_image = Image.new("RGB", (ww0, hh0), "white")
  20. pixel_data = []
  21.  
  22. def gen(w,h):
  23.     D = 20
  24.     t = [z for z in range(256)]
  25.     a = {}
  26.     for z in t:
  27.         v = min(z,256-D)
  28.         a[z] = range(v,v+D)
  29.     p = {}
  30.     w2 = w/255.0
  31.     h2 = h/255.0
  32.     rgb = []
  33.     for y in range(h):
  34.         cy = int(y/h2)
  35.         for x in range(w):
  36.             cx = int(x/w2)
  37.             r = 255-cx
  38.             g = 255-cy
  39.             b = int((cx+cy)/2)
  40.             for bbb in a[b]:
  41.                 z = (r,g,bbb)
  42.                 try:
  43.                     p[z]
  44.                 except:
  45.                     break
  46.             rgb.append(z)
  47.             p[z] = 1
  48.     return rgb
  49. pixel_data = gen(ww0,hh0)
  50. ref_image.putdata(pixel_data)
  51.  
  52. def update_params_x(value):
  53.     global x0
  54.     x0 = float(value)
  55.  
  56. def update_params_y(value):
  57.     global y0
  58.     y0 = float(value)
  59.  
  60. def update_params_z(value):
  61.     global z0
  62.     z0 = float(value)
  63.  
  64. x0 = 3
  65. y0 = 0
  66. z0 = 2
  67.  
  68. x_slider = tk.Scale(root, from_=10, to=0, label='X', resolution=0.001, orient=tk.VERTICAL, bg="#FFFF00")
  69. x_slider.set(x0)
  70.  
  71. y_slider = tk.Scale(root, from_=10, to=0, label='Y', resolution=0.001, orient=tk.VERTICAL, bg="#00FF00")
  72. y_slider.set(y0)
  73.  
  74. z_slider = tk.Scale(root, from_=10, to=0, label='Z', resolution=0.001, orient=tk.VERTICAL, bg="#00C0FF")
  75. z_slider.set(z0)
  76.  
  77. canvas.pack(side=tk.LEFT)
  78. x_slider.pack(side=tk.LEFT, fill='y')
  79. y_slider.pack(side=tk.LEFT, fill='y')
  80. z_slider.pack(side=tk.LEFT, fill='y')
  81.  
  82. x_slider.config(command=lambda value: update_params_x(x_slider.get()))
  83. y_slider.config(command=lambda value: update_params_y(y_slider.get()))
  84. z_slider.config(command=lambda value: update_params_z(z_slider.get()))
  85.  
  86. kernel_values = [0.05, 0.2, 0.05, 0.2, -1, 0.2, 0.05, 0.2, 0.05]
  87.  
  88. def display():
  89.     img = ref_image.crop((4, 4, ww + 4, hh + 4))
  90.     img_tk = ImageTk.PhotoImage(img)
  91.     canvas.create_image(0, 0, anchor="nw", image=img_tk)
  92.     canvas.img = img_tk
  93.     root.update()
  94.     ref_image.putpixel((center_x, center_y - 1), (255, 255, 255))
  95.     ref_image.putpixel((center_x, center_y + 1), (0, 0, 0))
  96.    
  97. kernel = ImageFilter.Kernel((3, 3), kernel_values, scale=None, offset=0)
  98. while True:
  99.     x, y, z = x0, y0, z0
  100.     ref_image = ref_image.rotate(-0.5, resample=Image.BICUBIC)  
  101.     ref_image = ref_image.resize((ww + 8, hh + 8), resample=Image.BICUBIC)
  102.     ref_image = ref_image.crop((4, 4, ww + 4, hh + 4))
  103.     ref_image = ref_image.filter(kernel)
  104.     ref_image = ref_image.filter(ImageFilter.GaussianBlur(x))
  105.     ref_image = ref_image.filter(kernel)
  106.     ref_image = ref_image.filter(ImageFilter.GaussianBlur(y))
  107.     ref_image = ref_image.filter(kernel)
  108.     ref_image = ref_image.filter(ImageFilter.GaussianBlur(z))
  109.     ref_image = ref_image.filter(kernel)
  110.     display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement