here2share

# tk_rgb_spiral_zoom.py

May 31st, 2023
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. # tk_rgb_spiral_zoom.py
  2.  
  3. # most colorful
  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_rgb_spiral_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. cx, cy = ww//2, hh//2
  19. img = Image.new('RGB', (ww, hh), "white")
  20.  
  21. rgb = []
  22. def create_rgb():
  23.     t = list(range(0, 256, 15))
  24.     rrr = t[:]
  25.     ggg = t[:]
  26.     bbb = t[:]
  27.     r = g = b = 0
  28.     while 1:
  29.         for r in rrr:
  30.             for g in ggg:
  31.                 for b in bbb:
  32.                     if (r,g,b) not in rgb:
  33.                         rgb.append((r,g,b))
  34.                     else:
  35.                         return
  36.                 if bbb.index(b) == len(bbb) - 1:
  37.                     bbb = bbb[::-1]
  38.             if ggg.index(g) == len(ggg) - 1:
  39.                 ggg = ggg[::-1]
  40.         rrr = rrr[::-1]
  41. create_rgb()
  42. L = len(rgb)
  43.  
  44. cXY = []
  45. t = ww//4
  46. for y in range(t,t*3):
  47.     for x in range(t,t*3):
  48.         distance = ((cx-x)**2+(cy-y)**2)**0.5
  49.         if distance < ww//4:
  50.             xy2 = math.atan2(x-cx,y-cy)
  51.             cXY.append(((int(distance), xy2),x,y))
  52. cXY.sort()
  53. cXY = [(x,y) for z,x,y in cXY]
  54. LXY = len(cXY)
  55.  
  56. blur_radius = 0.0005 * min(img.size)
  57. scale = 0
  58. zoom = 0.01
  59.  
  60. def draw():
  61.     x, y = cXY[i]
  62.     r, g, b = rgb.pop(0)
  63.     rgb.append((r, g, b))
  64.     rrr, ggg, bbb = img.getpixel((x,y))
  65.     r = rrr + max(-clamp, min(clamp, r - rrr))
  66.     g = ggg + max(-clamp, min(clamp, g - ggg))
  67.     b = bbb + max(-clamp, min(clamp, b - bbb))
  68.     img.putpixel((x, y+1), (r, g, b))
  69.  
  70.  
  71. clamp = 29
  72. while True:
  73.     if scale%20 < 20 and scale%20 + zoom > 20:
  74.         for i in range(0, LXY, 5):
  75.             draw()
  76.     else:
  77.         for i in range(0, LXY, 40):
  78.             draw()
  79.     cXY.append(cXY.pop(int(scale)%L))
  80.  
  81.     xt, yt = int(ww+scale), int(hh+scale)
  82.     img = img.resize((xt, yt), resample=Image.LANCZOS)
  83.     # img = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  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