Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_bruteforce_sq_spiral.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageFilter
- WW, HH = 600, 600
- cSq = 21
- pixels = cSq ** 2
- root = tk.Tk()
- canvas = tk.Canvas(root, width=WW, height=HH)
- root.geometry("%dx%d+600+10" % (WW, HH))
- canvas.pack()
- base = {
- (0, 0): (255, 255, 255), (0, 1): (255, 255, 0), (0, 2): (0, 255, 0),
- (1, 0): (255, 128, 0), (1, 1): (128, 128, 128), (1, 2): (0, 0, 255),
- (2, 0): (255, 0, 0), (2, 1): (128, 0, 128), (2, 2): (0, 0, 0)
- }
- def lerp(a, b, t):
- return a + (b - a) * t
- grid_colors = {}
- for i in range(cSq):
- for j in range(cSq):
- x = i * 2 / (cSq - 1)
- y = j * 2 / (cSq - 1)
- x0 = int(x)
- y0 = int(y)
- x1 = min(2, x0 + 1)
- y1 = min(2, y0 + 1)
- tx = x - x0
- ty = y - y0
- c00 = base[(x0, y0)]
- c10 = base[(x1, y0)]
- c01 = base[(x0, y1)]
- c11 = base[(x1, y1)]
- final = []
- for k in range(3):
- top = lerp(c00[k], c10[k], tx)
- bot = lerp(c01[k], c11[k], tx)
- val = lerp(top, bot, ty)
- final.append(int(val))
- grid_colors[(i, j)] = tuple(final)
- img = Image.new("RGB", (cSq, cSq))
- for i in range(cSq):
- for j in range(cSq):
- img.putpixel((i, j), grid_colors[i, j])
- def draw_img(img, radius=10):
- img0 = img.resize((600, 600), Image.BILINEAR)
- img0 = img0.filter(ImageFilter.GaussianBlur(radius))
- photo = ImageTk.PhotoImage(img0)
- canvas.image = photo
- canvas.create_image(0, 0, anchor="nw", image=photo)
- canvas.update()
- def spiral_coords(sz):
- coords = []
- xy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
- x, y = 0, 0
- xy_way = 0
- x0, y0 = xy[xy_way]
- while len(coords) < sz ** 2:
- coords.append((x, y))
- x2 = x + x0
- y2 = y + y0
- if min(x2, y2) < 0 or max(x2, y2) > (sz - 1) or (x2, y2) in coords:
- xy_way += 1
- xy_way %= 4
- x0, y0 = xy[xy_way]
- x += x0
- y += y0
- return coords
- coords = spiral_coords(cSq)
- draw_img(img)
- pixel_list = [img.getpixel((x, y)) for (x, y) in coords]
- coords = coords[::-1]
- gradient_cache = {}
- for i in range(len(pixel_list) - 1):
- gradient_cache[pixel_list[i]] = pixel_list[i + 1]
- gradient_cache[pixel_list[-1]] = pixel_list[0]
- img = img.resize((cSq, cSq), Image.BILINEAR)
- frame = 1
- while True:
- idx = 0
- for (x, y) in coords:
- if frame % (idx + 1) == 0:
- color = img.getpixel((x, y))
- img.putpixel((x, y), gradient_cache[color])
- idx += 1
- draw_img(img)
- frame += 1
- frame %= pixels
Advertisement
Add Comment
Please, Sign In to add comment