here2share

# tk_ultra_infinite_zoom_2.py

Jun 9th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. # tk_ultra_infinite_zoom_2.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_2")
  13. root.geometry("%dx%d+10+10"%(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.0001 * min(img.size)
  38.  
  39. scale = 29
  40. rrr = 27
  41. ggg = 11
  42. bbb = 21
  43. # Zoom effect
  44. while True:
  45.     for i in range(2500):
  46.         x, y = cXY.pop(0)
  47.         cXY.insert(-(i%10), (x, y))
  48.         r, g, b = img.getpixel((x,y))
  49.         if -1 < (r + rrr) < 256:
  50.             r = (r + rrr)
  51.         else:
  52.             rrr *= -1
  53.         if -1 < (g + ggg) < 256:
  54.             g = (g + ggg)
  55.         else:
  56.             ggg *= -1
  57.         if -1 < (b + bbb) < 256:
  58.             b = (b + bbb)
  59.         else:
  60.             bbb *= -1
  61.         xy0 = [(x+1, y), (x, y+1), (x-1, y), (x, y-1)][i%4]
  62.         img.putpixel(xy0, (r, g, b))
  63.  
  64.     img = img.resize((ww, hh), resample=Image.LANCZOS)
  65.     img = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  66.    
  67.     # crop parameters to zoom into the center
  68.     img = img.crop((scale/2, scale/2, ww-scale/2, hh-scale/2))
  69.    
  70.     tkimg = ImageTk.PhotoImage(img)
  71.     canvas.create_image((cx, cy), image=tkimg)
  72.     canvas.update()
Add Comment
Please, Sign In to add comment