Advertisement
here2share

# tk_blurred_zoom.py

Sep 1st, 2024
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. # tk_blurred_zoom.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw
  5. import math
  6. import random
  7.  
  8. WW, HH = 600, 600
  9. WC, HC = WW // 2, HH // 2
  10. root = tk.Tk()
  11. root.title("# tk_blurred_zoom")
  12. canvas = tk.Canvas(root, width=WW, height=HH, bg='black')
  13. root.geometry("%dx%d+10+10" % (WW, HH))
  14. canvas.pack()
  15.  
  16. scale_factor = 1.5
  17. number_of_points = 12
  18. tag_id = {}
  19. points = []
  20.  
  21. def capture_canvas():
  22.     image = Image.new("RGBA", (WW, HH), (0, 0, 0, 0))
  23.     draw = ImageDraw.Draw(image)
  24.  
  25.     for item in canvas.find_all():
  26.         item_type = canvas.type(item)
  27.         coords = canvas.coords(item)
  28.  
  29.         if item_type == "polygon":
  30.             color = canvas.itemcget(item, "fill")
  31.             draw.polygon(coords, fill=color)
  32.  
  33.     return image
  34.  
  35. def make_polygon(args):
  36.     r0, g0, b0, points = args
  37.     tag_id[i] = canvas.create_polygon(points, fill="#{:02x}{:02x}{:02x}".format(r0, g0, b0), smooth=True)
  38.  
  39. def blob():
  40.     del points[:]
  41.     r0, g0, b0 = [random.randint(0, 255) for _ in range(3)]
  42.     for j in range(number_of_points):
  43.         angle = 2 * math.pi * j / number_of_points
  44.         r = random.uniform(1, 0.25)
  45.         a = angle * random.uniform(0.95, 1)
  46.         x = WC + r * math.cos(a)
  47.         y = HC + r * math.sin(a)
  48.         points.extend((x, y))
  49.     return r0, g0, b0, points
  50.  
  51. def plot():
  52.     for _ in range(5):
  53.         for j in tag_ids:
  54.             try:
  55.                 x1, y1, x2, y2 = canvas.bbox(tag_id[j])
  56.                 center_x = (x1 + x2) / 2
  57.                 center_y = (y1 + y2) / 2
  58.                 new_width = (x2 - x1) * scale_factor
  59.                 new_height = (y2 - y1) * scale_factor
  60.                 delta_x = (new_width - (x2 - x1)) / 2
  61.                 delta_y = (new_height - (y2 - y1)) / 2
  62.                 canvas.scale(tag_id[j], center_x, center_y, scale_factor, scale_factor)
  63.             except:
  64.                 tag_ids.remove(j)
  65.            
  66.         img = capture_canvas()
  67.         img = img.filter(ImageFilter.GaussianBlur(radius=25))
  68.         canvas.image = ImageTk.PhotoImage(img)
  69.         canvas.create_image(0, 0, anchor=tk.NW, image=canvas.image)
  70.         canvas.update()
  71.  
  72.  
  73. i = 0
  74. tag_ids = [i]
  75. while 1:
  76.     make_polygon(blob())
  77.     plot()
  78.     i += 1
  79.     tag_ids += [i]
  80.  
  81. root.mainloop()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement