here2share

# tk_rgb_spiral_test_2.py

May 28th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. # tk_rgb_spiral_test_2.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  5. import math
  6.  
  7. ww = 640
  8. hh = 640
  9.  
  10. root = tk.Tk()
  11. root.title("tk_rgb_spiral_test")
  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, 15))
  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. while True:
  53.     for i in range(LXY):
  54.         x, y = cXY[i]
  55.         img.putpixel((x, y), rgb[i%L])
  56.  
  57.     a = rgb[::2]
  58.     b = rgb[1::2]
  59.     rgb = []
  60.     rgb.extend(a)
  61.     rgb.extend(b)
  62.    
  63.     tkimg = ImageTk.PhotoImage(img)
  64.     canvas.create_image((cx, cy), image=tkimg)
  65.     canvas.update()
Add Comment
Please, Sign In to add comment