Advertisement
here2share

# tk_rgb_oops.py

May 18th, 2023 (edited)
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. # tk_rgb_oops.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk
  5. import math
  6. import random
  7.  
  8. ww = 300
  9. hh = 300
  10.  
  11. root = tk.Tk()
  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. def xy2deg(x, y):
  17.     return math.degrees(math.atan2(x - cx, y - cy))
  18.  
  19. rgb = [i for i in range(0, 256, 2)]
  20. COLORS = [(r,g,b) for r in rgb for g in rgb for b in rgb]
  21. cx, cy = ww//2, hh//2
  22.  
  23. dist_dict = {}
  24. buffer = []
  25. for i in range(ww*hh):
  26.     x = i % ww
  27.     y = i // ww
  28.     dx = cx - x
  29.     dy = cy - y
  30.    
  31.     distance = dx**2 + dy**2
  32.     try:
  33.         dist_dict[distance] += 0.1
  34.     except:
  35.         dist_dict[distance] = distance
  36.     buffer.append(([xy2deg(x, y), dist_dict[distance]], i))
  37.  
  38. buffer.sort()
  39. buffer = [x[1] for x in buffer]
  40.  
  41. # test
  42. img = Image.new('RGB', (ww, hh), "white")
  43. Lc = len(COLORS)
  44. CvSz = ww*hh
  45.  
  46. for i in range(Lc-CvSz):
  47.     pixels = COLORS[i:i+CvSz]
  48.     pixels = [pixels[j] for j in buffer]
  49.     img.putdata(pixels)
  50.     tkimg = ImageTk.PhotoImage(img)
  51.     canvas.create_image((cx, cy), image=tkimg)
  52.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement