Advertisement
here2share

# tk_flood_zoom.py

Mar 8th, 2025 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. # tk_flood_zoom.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk
  5. import math
  6. import random
  7.  
  8. WW, HH = 640, 640
  9. CX, CY = WW // 2, HH // 2
  10.  
  11. rnd = 256 / 15
  12.  
  13. root = tk.Tk()
  14. root.title("# tk_flood_zoom.py")
  15. canvas = tk.Canvas(root, width=WW, height=HH)
  16. root.geometry("+10+10")
  17. canvas.pack()
  18.  
  19. img = Image.new('RGB', (WW, HH), "black")
  20.  
  21. rgb_list = {}
  22. rgb_list_max = {}
  23. step = 15
  24. max_value = 1000
  25. for r in range(0, 256, step):
  26.     for g in range(0, 256, step):
  27.         for b in range(0, 256, step):
  28.             rgb_list[(r, g, b)] = max_value
  29. Lrgb = len(rgb_list)
  30. rgb_list_max = rgb_list.copy()
  31. rgb_cue = list(rgb_list)
  32.  
  33. def interpolate_color(color1, color2, factor):
  34.     return tuple(int(color1[i] + (color2[i] - color1[i]) * factor) for i in range(3))
  35.  
  36. grid_colors = {
  37.     (0, 0): (255, 255, 255),
  38.     (0, 1): (255, 255, 0),
  39.     (0, 2): (0, 255, 0),
  40.     (1, 0): (255, 165, 0),
  41.     (1, 1): (128, 128, 128),
  42.     (1, 2): (0, 0, 255),
  43.     (2, 0): (255, 0, 0),
  44.     (2, 1): (128, 0, 128),
  45.     (2, 2): (0, 0, 0)
  46. }
  47.  
  48. pixel_data = []
  49. cXY = []
  50. inner = {}
  51. for j in range(HH):
  52.     for i in range(WW):
  53.         x = i / (WW - 1) * 2
  54.         y = j / (HH - 1) * 2
  55.         x0, y0 = int(x), int(y)
  56.         x1, y1 = min(x0 + 1, 2), min(y0 + 1, 2)
  57.         fx, fy = x - x0, y - y0
  58.         color_top = interpolate_color(grid_colors[(x0, y0)], grid_colors[(x1, y0)], fx)
  59.         color_bottom = interpolate_color(grid_colors[(x0, y1)], grid_colors[(x1, y1)], fx)
  60.         color = interpolate_color(color_top, color_bottom, fy)
  61.         pixel_data.append(color)
  62.  
  63.         distance = ((CX - i) ** 2 + (CY - j) ** 2) ** 0.5
  64.         xy2 = math.atan2(i - CX, j - CY)
  65.         if distance < 60:
  66.             cXY.append(((int(distance), xy2), i, j))
  67.             if distance < 2:
  68.                 inner[(i, j)] = 0
  69.  
  70. cXY.sort()
  71. cXY = [(x, y) for z, x, y in cXY]
  72. LXY = len(cXY)
  73.  
  74. img.putdata(pixel_data)
  75.  
  76. def call_rgb(rgb):
  77.     rgb = tuple([(value + 1) // step * step for value in rgb])
  78.     return rgb_list[rgb], rgb
  79.  
  80. def plot(i):
  81.     for j in range(i, LXY, n * 2):
  82.         x, y = cXY[j]
  83.        
  84.         if (x, y) in inner:
  85.             rgb = random.choice(rgb_cue)
  86.         else:
  87.             neighbors = [img.getpixel((x + x0, y + y0)) for x0 in (-1, 1) for y0 in (-1, 1)]
  88.             value, rgb = min([call_rgb(rgb) for rgb in neighbors])
  89.             if value > 0:
  90.                 value = value - 1
  91.             else:
  92.                 rgb_list_max[rgb] += 5
  93.                 value = rgb_list_max[rgb]
  94.            
  95.             rgb_list[rgb] = value
  96.            
  97.         img.putpixel((x, y), rgb)
  98.  
  99. scale = 20
  100. n = 6
  101. oXY = [i if i % 2 else n * 2 - i - 2 for i in range(n * 2)]
  102. while True:
  103.     for i in oXY:
  104.         canvas.delete('all')
  105.         img = img.crop((scale, scale, WW - scale, HH - scale))
  106.         img = img.resize((WW, HH))
  107.         photo = ImageTk.PhotoImage(img)
  108.         canvas.create_image(0, 0, image=photo, anchor=tk.NW)
  109.         root.update()
  110.  
  111.         plot(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement