here2share

# tk_rgb_whirlpool.py

May 28th, 2023
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. # tk_rgb_whirlpool.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  5. import math
  6.  
  7. ww = 250
  8. hh = 250
  9.  
  10. root = tk.Tk()
  11. root.title("tk_rgb_whirlpool")
  12. root.geometry("%dx%d+0+0"%(ww,hh))
  13. canvas = tk.Canvas(root, bg='white', width=ww, height=hh)
  14. canvas.pack()
  15.  
  16. cx, cy = ww//2, hh//2
  17. img = Image.new('RGB', (ww, hh), "white")
  18.  
  19. rgb = []
  20. def create_rgb():
  21.     t = list(range(0, 256, 50))
  22.     rrr = t[:]
  23.     ggg = t[:]
  24.     bbb = t[:]
  25.     r = g = b = 0
  26.     while 1:
  27.         for r in rrr:
  28.             for g in ggg:
  29.                 for b in bbb:
  30.                     if (r,g,b) not in rgb:
  31.                         rgb.append((r,g,b))
  32.                     else:
  33.                         return
  34.                 if bbb.index(b) == len(bbb) - 1:
  35.                     bbb = bbb[::-1]
  36.             if ggg.index(g) == len(ggg) - 1:
  37.                 ggg = ggg[::-1]
  38.         rrr = rrr[::-1]
  39. create_rgb()
  40. L = len(rgb)
  41.  
  42. cXY = []
  43. for y in range(hh):
  44.     for x in range(ww):
  45.         distance = ((cx-x)**2+(cy-y)**2)**0.5
  46.         xy2 = math.atan2(x-cx,y-cy)
  47.         cXY.append(((int(distance), xy2),x,y))
  48. cXY.sort()
  49. cXY = [(x,y) for z,x,y in cXY]
  50. LXY = len(cXY)
  51.  
  52. j = 0
  53. while True:
  54.     for i in range(LXY):
  55.         x, y = cXY[i]
  56.         img.putpixel((x, y), rgb[i%L])
  57.  
  58.     t = rgb.pop(0)
  59.     rgb.insert(-j, t)
  60.     j = (j+1)%10
  61.    
  62.     tkimg = ImageTk.PhotoImage(img)
  63.     canvas.create_image((cx, cy), image=tkimg)
  64.     canvas.update()
Add Comment
Please, Sign In to add comment