here2share

# tk_rnd_interweave.py

Oct 8th, 2025
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. # tk_rnd_interweave.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageDraw, ImageTk
  5. import random
  6. import math
  7.  
  8. w, h = 600, 600
  9. grid_x = 40
  10. grid_y = 40
  11. grid_x_pixels = 1.1 * w
  12. grid_y_pixels = 1.1 * h
  13. sep_x = grid_x_pixels / (grid_x - 1)
  14. sep_y = grid_y_pixels / (grid_y - 1)
  15.  
  16. colors = []
  17.  
  18. def get_midpoint(x1, y1, x2, y2):
  19.     return [(x1 + x2)/2, (y1 + y2)/2]
  20.  
  21. def get_distance(x1, y1, x2, y2):
  22.     return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
  23.  
  24. def brush_points(draw, x1, y1, x2, y2, brush_height, brush_sat, brush_drift, brush_tips, c):
  25.     mp = get_midpoint(x1, y1, x2, y2)
  26.     d = get_distance(x1, y1, x2, y2)
  27.  
  28.     angle = random.random() * 2 * math.pi
  29.     cos_a = math.cos(angle)
  30.     sin_a = math.sin(angle)
  31.  
  32.     for i in range(-brush_height, brush_height, brush_sat):
  33.         fi = random.uniform(-brush_drift, brush_drift)
  34.         se = random.uniform(-brush_drift, brush_drift)
  35.         th = random.uniform(-brush_drift, brush_drift)
  36.         be = random.uniform(-brush_tips, brush_tips)
  37.         en = random.uniform(-brush_tips, brush_tips)
  38.  
  39.         # Rotate points around origin
  40.         def rotate(px, py):
  41.             return (px * cos_a - py * sin_a, px * sin_a + py * cos_a)
  42.  
  43.         p1 = rotate(-d/2 + be, i + fi)
  44.         p2 = rotate(0, i + se)
  45.         p3 = rotate(d/2 + en, i + th)
  46.  
  47.         draw.line([ (x1 + p1[0], y1 + p1[1]),
  48.                     (x1 + p2[0], y1 + p2[1]),
  49.                     (x1 + p3[0], y1 + p3[1]) ],
  50.                   fill=c, width=1)
  51.  
  52. def generate_image():
  53.     img = Image.new("RGB", (w, h), (0, 0, 0))
  54.     draw = ImageDraw.Draw(img)
  55.  
  56.     colors.clear()
  57.     start_c = random.randint(0, 360)
  58.     for i in range(8):
  59.         hue = (start_c + i * 15) % 360
  60.         # Convert HSB to RGB (approximate)
  61.         rgb = hsb_to_rgb(hue, 0.3, 1.0)
  62.         colors.append(rgb)
  63.  
  64.     current_x = w/2.0 - grid_x_pixels/2.0
  65.     current_y = h/2.0 - grid_y_pixels/2.0
  66.     for i in range(grid_x):
  67.         for j in range(grid_y):
  68.             brush_points(draw,
  69.                          current_x, current_y,
  70.                          current_x + sep_x * random.uniform(1, 2),
  71.                          current_y + sep_y * random.uniform(1, 2),
  72.                          int(sep_x/3), 1, 7, 8,
  73.                          random.choice(colors))
  74.             current_y += sep_y
  75.         current_y = h/2.0 - grid_y_pixels/2.0
  76.         current_x += sep_x
  77.  
  78.     return img
  79.  
  80. def hsb_to_rgb(h, s, b):
  81.     h = float(h)
  82.     s = float(s)
  83.     b = float(b)
  84.     h = h % 360
  85.     c = b * s
  86.     x = c * (1 - abs((h / 60) % 2 - 1))
  87.     m = b - c
  88.     if h < 60:
  89.         r, g, b = c, x, 0
  90.     elif h < 120:
  91.         r, g, b = x, c, 0
  92.     elif h < 180:
  93.         r, g, b = 0, c, x
  94.     elif h < 240:
  95.         r, g, b = 0, x, c
  96.     elif h < 300:
  97.         r, g, b = x, 0, c
  98.     else:
  99.         r, g, b = c, 0, x
  100.     return (int((r + m) * 255), int((g + m) * 255), int((b + m) * 255))
  101.  
  102. def redraw(event=None):
  103.     img = generate_image()
  104.     tk_img = ImageTk.PhotoImage(img)
  105.     canvas.img = tk_img
  106.     canvas.create_image(0, 0, anchor=tk.NW, image=tk_img)
  107.  
  108. # Tkinter setup
  109. root = tk.Tk()
  110. root.title("Brush Grid")
  111. canvas = tk.Canvas(root, width=w, height=h)
  112. canvas.pack()
  113. root.bind("<space>", redraw)
  114.  
  115. redraw()
  116. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment