here2share

# tk_bruteforce_sq_spiral.py

Jul 8th, 2026 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. # tk_bruteforce_sq_spiral.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageFilter
  5.  
  6. WW, HH = 600, 600
  7. cSq = 21
  8. pixels = cSq ** 2
  9.  
  10. root = tk.Tk()
  11. canvas = tk.Canvas(root, width=WW, height=HH)
  12. root.geometry("%dx%d+600+10" % (WW, HH))
  13. canvas.pack()
  14.  
  15. base = {
  16.     (0, 0): (255, 255, 255),   (0, 1): (255, 255,   0),   (0, 2): (0, 255,   0),
  17.     (1, 0): (255, 128,   0),   (1, 1): (128, 128, 128),   (1, 2): (0,   0, 255),
  18.     (2, 0): (255,   0,   0),   (2, 1): (128,   0, 128),   (2, 2): (0,   0,   0)
  19. }
  20.  
  21. def lerp(a, b, t):
  22.     return a + (b - a) * t
  23.  
  24. grid_colors = {}
  25.  
  26. for i in range(cSq):
  27.     for j in range(cSq):
  28.         x = i * 2 / (cSq - 1)
  29.         y = j * 2 / (cSq - 1)
  30.  
  31.         x0 = int(x)
  32.         y0 = int(y)
  33.         x1 = min(2, x0 + 1)
  34.         y1 = min(2, y0 + 1)
  35.  
  36.         tx = x - x0
  37.         ty = y - y0
  38.  
  39.         c00 = base[(x0, y0)]
  40.         c10 = base[(x1, y0)]
  41.         c01 = base[(x0, y1)]
  42.         c11 = base[(x1, y1)]
  43.  
  44.         final = []
  45.         for k in range(3):
  46.             top = lerp(c00[k], c10[k], tx)
  47.             bot = lerp(c01[k], c11[k], tx)
  48.             val = lerp(top, bot, ty)
  49.             final.append(int(val))
  50.  
  51.         grid_colors[(i, j)] = tuple(final)
  52.  
  53. img = Image.new("RGB", (cSq, cSq))
  54.  
  55. for i in range(cSq):
  56.     for j in range(cSq):
  57.         img.putpixel((i, j), grid_colors[i, j])
  58.  
  59. def draw_img(img, radius=10):
  60.     img0 = img.resize((600, 600), Image.BILINEAR)
  61.     img0 = img0.filter(ImageFilter.GaussianBlur(radius))
  62.     photo = ImageTk.PhotoImage(img0)
  63.     canvas.image = photo
  64.     canvas.create_image(0, 0, anchor="nw", image=photo)
  65.     canvas.update()
  66.  
  67. def spiral_coords(sz):
  68.     coords = []
  69.     xy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
  70.     x, y = 0, 0
  71.     xy_way = 0
  72.     x0, y0 = xy[xy_way]
  73.  
  74.     while len(coords) < sz ** 2:
  75.         coords.append((x, y))
  76.         x2 = x + x0
  77.         y2 = y + y0
  78.         if min(x2, y2) < 0 or max(x2, y2) > (sz - 1) or (x2, y2) in coords:
  79.             xy_way += 1
  80.             xy_way %= 4
  81.             x0, y0 = xy[xy_way]
  82.         x += x0
  83.         y += y0
  84.    
  85.     return coords
  86. coords = spiral_coords(cSq)
  87.        
  88. draw_img(img)
  89.  
  90. pixel_list = [img.getpixel((x, y)) for (x, y) in coords]
  91. coords = coords[::-1]
  92.  
  93. gradient_cache = {}
  94. for i in range(len(pixel_list) - 1):
  95.     gradient_cache[pixel_list[i]] = pixel_list[i + 1]
  96. gradient_cache[pixel_list[-1]] = pixel_list[0]
  97.  
  98. img = img.resize((cSq, cSq), Image.BILINEAR)
  99.  
  100. frame = 1
  101. while True:
  102.     idx = 0
  103.     for (x, y) in coords:
  104.         if frame % (idx + 1) == 0:
  105.             color = img.getpixel((x, y))
  106.             img.putpixel((x, y), gradient_cache[color])
  107.         idx += 1
  108.     draw_img(img)
  109.     frame += 1
  110.     frame %= pixels
Advertisement
Add Comment
Please, Sign In to add comment