here2share

# tk_flippant_spiral_grayscale.py

Jan 4th, 2026
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. # tk_flippant_spiral_grayscale.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  5. import math
  6.  
  7. cell_size = 20
  8. SQ = 400 // cell_size * cell_size
  9. GRID = SQ // cell_size
  10. CENTER = (GRID - 1) / 2
  11.  
  12. rr = tk.Tk()
  13. rr.geometry('+0+0')
  14.  
  15. cv = tk.Canvas(rr, width=SQ, height=SQ, bg='black')
  16. cv.pack(side='left')
  17.  
  18. canvas_image_id = cv.create_image(0, 0, anchor='nw')
  19.  
  20. MIN_SPEED = 0.0001
  21. MAX_SPEED = 5.0
  22.  
  23. grays = []
  24. def z(r, g, b):
  25.     grays.append(f"#{r:02X}{g:02X}{b:02X}")
  26. r, g, b = 255, 0, 0
  27. for i in range(256):
  28.     z(i, i, i)
  29. grays = grays[::-1] + grays[1:-1]
  30.  
  31. lc = len(grays) - 1
  32.  
  33. img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
  34. draw = ImageDraw.Draw(img)
  35.  
  36. max_dist = math.hypot(CENTER, CENTER)
  37. order = sorted(
  38.     [(x, y) for y in range(GRID) for x in range(GRID)],
  39.     key=lambda xy: (
  40.         math.hypot(xy[0] - CENTER, xy[1] - CENTER) / max_dist,
  41.         math.atan2(xy[1] - CENTER, xy[0] - CENTER)
  42.     )
  43. )
  44.  
  45. speeds = [0.0] * (GRID * GRID)
  46. for i, (x, y) in enumerate(order):
  47.     speeds[y * GRID + x] = MIN_SPEED + (MAX_SPEED - MIN_SPEED) * (1.0 - i / ((GRID * GRID) - 1))
  48.  
  49. cells = [0.0] * (GRID * GRID)
  50.  
  51. while 1:
  52.     i = 0
  53.     for y in range(GRID):
  54.         for x in range(GRID):
  55.             cells[i] += speeds[i]
  56.  
  57.             if cells[i] >= lc:
  58.                 cells[i] = lc
  59.                 speeds[i] *= -1
  60.             elif cells[i] <= 0:
  61.                 cells[i] = 0
  62.                 speeds[i] *= -1
  63.  
  64.             col = grays[int(cells[i])]
  65.             x0 = x * cell_size
  66.             y0 = y * cell_size
  67.  
  68.             draw.rectangle(
  69.                 (x0, y0, x0 + cell_size, y0 + cell_size),
  70.                 fill=col
  71.             )
  72.            
  73.             i += 1
  74.  
  75.     img_out = img.filter(ImageFilter.GaussianBlur(radius=50 * 0.2))
  76.  
  77.     tk_img = ImageTk.PhotoImage(img_out)
  78.     cv.itemconfig(canvas_image_id, image=tk_img)
  79.     cv.tk_img = tk_img
  80.  
  81.     rr.update_idletasks()
  82.     rr.update()
  83.  
Advertisement
Add Comment
Please, Sign In to add comment