Advertisement
here2share

# tk_ultra_infinite_zoom.py

Jun 7th, 2023
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. # tk_ultra_infinite_zoom.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  5. import math
  6. import random
  7.  
  8. ww = 600
  9. hh = 600
  10.  
  11. root = tk.Tk()
  12. root.title("tk_ultra_infinite_zoom")
  13. root.geometry("%dx%d+0+0"%(ww,hh))
  14. canvas = tk.Canvas(root, bg='white', width=ww, height=hh)
  15. canvas.pack()
  16.  
  17. cx, cy = ww//2, hh//2
  18. img = Image.new('RGB', (ww, hh), "white")
  19.  
  20. pixels = []
  21.  
  22. t = 100
  23. cXY = [(x, y) for x in range(cx-t, cx+t) for y in range(cy-t, cy+t)]
  24. random.shuffle(cXY)
  25.  
  26. for y in range(hh):
  27.     for x in range(ww):
  28.         r, g, b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
  29.         pixels.append((r, g, b))
  30.  
  31. img.putdata(pixels)
  32. tkimg = ImageTk.PhotoImage(img)
  33. canvas.create_image((cx, cy), image=tkimg)
  34. canvas.update()
  35.  
  36. # calculate the blur radius to achieve 0.01% blur
  37. blur_radius = 0.0003 * min(img.size)
  38.  
  39. scale = 15
  40. rnd = 50
  41. # Zoom effect
  42. while True:
  43.     for i in range(1000):
  44.         x, y = cXY.pop(0)
  45.         cXY.insert(-(10-(i%10)), (x, y))
  46.         r, g, b = img.getpixel((x,y))
  47.         r = max(0, min(255, r + random.randint(-rnd, rnd)))
  48.         g = max(0, min(255, g + random.randint(-rnd, rnd)))
  49.         b = max(0, min(255, b + random.randint(-rnd, rnd)))
  50.         img.putpixel((x, y), (r, g, b))
  51.  
  52.     img = img.resize((ww, hh), resample=Image.LANCZOS)
  53.     img = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  54.    
  55.     # crop parameters to zoom into the center
  56.     img = img.crop((scale/2, scale/2, ww-scale/2, hh-scale/2))
  57.    
  58.     tkimg = ImageTk.PhotoImage(img)
  59.     canvas.create_image((cx, cy), image=tkimg)
  60.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement