Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_rgb_oops.py
- import tkinter as tk
- from PIL import Image, ImageTk
- import math
- import random
- ww = 300
- hh = 300
- root = tk.Tk()
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = tk.Canvas(root, bg='white', width=ww, height=hh)
- canvas.pack()
- def xy2deg(x, y):
- return math.degrees(math.atan2(x - cx, y - cy))
- rgb = [i for i in range(0, 256, 2)]
- COLORS = [(r,g,b) for r in rgb for g in rgb for b in rgb]
- cx, cy = ww//2, hh//2
- dist_dict = {}
- buffer = []
- for i in range(ww*hh):
- x = i % ww
- y = i // ww
- dx = cx - x
- dy = cy - y
- distance = dx**2 + dy**2
- try:
- dist_dict[distance] += 0.1
- except:
- dist_dict[distance] = distance
- buffer.append(([xy2deg(x, y), dist_dict[distance]], i))
- buffer.sort()
- buffer = [x[1] for x in buffer]
- # test
- img = Image.new('RGB', (ww, hh), "white")
- Lc = len(COLORS)
- CvSz = ww*hh
- for i in range(Lc-CvSz):
- pixels = COLORS[i:i+CvSz]
- pixels = [pixels[j] for j in buffer]
- img.putdata(pixels)
- tkimg = ImageTk.PhotoImage(img)
- canvas.create_image((cx, cy), image=tkimg)
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement