Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_fading_hue_cycle_pad.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageDraw, ImageFilter
- import math
- import time
- N = 180
- SCALE = 4
- P = 8
- FULL_N = N + P * 2
- PADDED_SIZE = FULL_N
- ellipse_size = 20
- blur_kernel = ImageFilter.Kernel((3, 3), [1] * 9, scale=9)
- def hsv_to_rgb(h, s, v):
- h = float(h)
- s = float(s)
- v = float(v)
- h60 = h / 60.0
- h60f = math.floor(h60)
- hi = int(h60f) % 6
- f = h60 - h60f
- p = v * (1 - s)
- q = v * (1 - f * s)
- t = v * (1 - (1 - f) * s)
- if hi == 0:
- r, g, b = v, t, p
- elif hi == 1:
- r, g, b = q, v, p
- elif hi == 2:
- r, g, b = p, v, t
- elif hi == 3:
- r, g, b = p, q, v
- elif hi == 4:
- r, g, b = t, p, v
- else:
- r, g, b = v, p, q
- return int(r*255), int(g*255), int(b*255)
- # Precompute hue table
- hue_cache = [hsv_to_rgb(i, 1.0, 1.0) for i in range(360)]
- # Create RGBA image
- size = 511
- flow_field = Image.new("RGBA", (size, size), (0, 0, 0, 0))
- pixels = flow_field.load()
- cx = cy = size // 2
- radius = 255
- for y in range(size):
- for x in range(size):
- dx = x - cx
- dy = y - cy
- distance = math.sqrt(dx*dx + dy*dy)
- angle = (math.degrees(math.atan2(dy, dx)) + 360) % 360
- r, g, b = hue_cache[int(angle)]
- alpha = int(255 - distance / 2)
- pixels[x, y] = (r, g, b, alpha)
- color_sources = [Image.new('RGB', (PADDED_SIZE, PADDED_SIZE), hue_cache[h]) for h in range(360)]
- black_image = Image.new('RGB', (PADDED_SIZE, PADDED_SIZE), (0, 0, 0))
- mask_cache = {}
- point_mask_cache = {}
- root = tk.Tk()
- density = Image.new('RGB', (PADDED_SIZE, PADDED_SIZE), (0, 0, 0))
- canvas = tk.Canvas(root, width=N*SCALE, height=N*SCALE)
- canvas.pack()
- img = density.crop((P, P, P+N, P+N)).resize((N*SCALE, N*SCALE), Image.NEAREST)
- photo = ImageTk.PhotoImage(img)
- img_id = canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- last = None
- last_fade_time = time.time()
- def fade_to_black():
- global density, last_fade_time
- current_time = time.time()
- if current_time - last_fade_time >= 0.:
- density = Image.blend(density, black_image, 0.01)
- last_fade_time = current_time
- def step():
- global density
- density = density.filter(blur_kernel)
- def render():
- return density.crop((P, P, P+N, P+N)).resize((N*SCALE, N*SCALE), Image.NEAREST)
- def add_ellipse_at_point(x, y, hue_int):
- """Add an ellipse at the given point"""
- global density
- px, py = x + P, y + P
- # Create cache key for this point and hue
- key = (px, py, hue_int)
- if key not in point_mask_cache:
- # Create a mask with an ellipse at the point
- mask = Image.new('L', (PADDED_SIZE, PADDED_SIZE), 0)
- draw = ImageDraw.Draw(mask)
- # Draw ellipse (circle) with radius 4
- draw.ellipse([(px - ellipse_size//2, py - ellipse_size//2),
- (px + ellipse_size//2, py + ellipse_size//2)], fill=255)
- point_mask_cache[key] = mask
- # Composite the colored ellipse onto the density
- density = Image.composite(color_sources[hue_int], density, point_mask_cache[key])
- def on_drag(e):
- x = max(0, min(N-1, e.x // SCALE))
- y = max(0, min(N-1, e.y // SCALE))
- hue_int = int((time.time() * 40) % 360)
- global density, last
- # Add ellipse at the current point
- add_ellipse_at_point(x, y, hue_int)
- if last:
- lx, ly = last
- px1, py1 = lx + P, ly + P
- px2, py2 = x + P, y + P
- key = (px1, py1, px2, py2)
- if key not in mask_cache:
- mask = Image.new('L', (PADDED_SIZE, PADDED_SIZE), 0)
- draw = ImageDraw.Draw(mask)
- draw.line([(px1, py1), (px2, py2)], fill=255, width=ellipse_size, joint='curve')
- mask_cache[key] = mask
- density = Image.composite(color_sources[hue_int], density, mask_cache[key])
- last = (x, y)
- def on_release(e):
- global last
- last = None
- def update():
- step()
- fade_to_black()
- img = render()
- photo = ImageTk.PhotoImage(img)
- canvas.itemconfig(img_id, image=photo)
- canvas.image = photo
- root.after(1, update)
- canvas.bind("<B1-Motion>", on_drag)
- canvas.bind("<ButtonRelease-1>", on_release)
- update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment