here2share

# tk_seq_infinite_zoom.py

May 28th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. # tk_seq_infinite_zoom.py
  2.  
  3. # without random
  4.  
  5. import tkinter as tk
  6. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  7. import math
  8.  
  9. ww = 640
  10. hh = 640
  11.  
  12. root = tk.Tk()
  13. root.title("tk_seq_infinite_zoom")
  14. root.geometry("%dx%d+0+0"%(ww,hh))
  15. canvas = tk.Canvas(root, bg='white', width=ww, height=hh)
  16. canvas.pack()
  17.  
  18. scale = 0
  19. zoom = 0.25
  20.  
  21. cx, cy = ww//2, hh//2
  22. img = Image.new('RGB', (ww, hh), "white")
  23.  
  24. rgb = []
  25. def create_rgb():
  26.     t = list(range(0, 256, 50))
  27.     rrr = t[:]
  28.     ggg = t[:]
  29.     bbb = t[:]
  30.     r = g = b = 0
  31.     while 1:
  32.         for r in rrr:
  33.             for g in ggg:
  34.                 for b in bbb:
  35.                     if (r,g,b) not in rgb:
  36.                         rgb.append((r,g,b))
  37.                     else:
  38.                         return
  39.                 if bbb.index(b) == len(bbb) - 1:
  40.                     bbb = bbb[::-1]
  41.             if ggg.index(g) == len(ggg) - 1:
  42.                 ggg = ggg[::-1]
  43.         rrr = rrr[::-1]
  44. create_rgb()
  45. L = len(rgb)
  46. pixels = []
  47.  
  48. t = 90
  49. cXY = []
  50. for y in range(cy-t, cy+t):
  51.     for x in range(cx-t, cx+t):
  52.         distance = ((cx-x)**2+(cy-y)**2)**0.5
  53.         if distance < t+1:
  54.             xy2 = math.atan2(x-cx,y-cy)
  55.             cXY.append(((int(distance), xy2),x,y))
  56. cXY.sort()
  57. cXY = [(x,y) for z,x,y in cXY]
  58.  
  59. for i, (x, y) in enumerate(cXY):
  60.     img.putpixel((x, y), rgb[i%L])
  61.  
  62. tkimg = ImageTk.PhotoImage(img)
  63. canvas.create_image((cx, cy), image=tkimg)
  64. canvas.update()
  65.  
  66. # Zoom effect
  67. clamp = 5
  68. while True:
  69.     if scale > 20:
  70.         for i in range(0, len(cXY), 5):
  71.             x, y = cXY[i]
  72.             r, g, b = rgb.pop(0)
  73.             rgb.insert(-(10-(i%10)), (r, g, b))
  74.             rrr, ggg, bbb = img.getpixel((x,y))
  75.             r = rrr + max(-clamp, min(clamp, r - rrr))
  76.             g = ggg + max(-clamp, min(clamp, g - ggg))
  77.             b = bbb + max(-clamp, min(clamp, b - bbb))
  78.             img.putpixel((x, y), (r, g, b))
  79.         t = cXY.pop(0)
  80.         cXY.append(t)
  81.    
  82.     xt, yt = int(ww+scale), int(hh+scale)
  83.     img = img.resize((xt, yt), resample=Image.LANCZOS)
  84.    
  85.     # crop parameters to zoom into the center
  86.     img = img.crop((scale/2, scale/2, xt-scale/2, yt-scale/2))
  87.     scale += zoom
  88.    
  89.     tkimg = ImageTk.PhotoImage(img)
  90.     canvas.create_image((cx, cy), image=tkimg)
  91.     canvas.update()
Add Comment
Please, Sign In to add comment