Advertisement
here2share

# tk_slider_art_generator.py

Jan 20th, 2025
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. # tk_slider_art_generator.py
  2. ### ZZZ todo: try to find a far more efficent way for it to generate the given pattern whereas entering any number will determine it
  3.  
  4. from tkinter import Tk, Canvas
  5. from PIL import Image, ImageTk, ImageDraw, ImageFont, ImageFilter
  6. import itertools
  7.  
  8. img_size = 400
  9.  
  10. root = Tk()
  11. root.title("# tk_slider_art_generator.py")
  12. canvas = Canvas(root, width=img_size, height=img_size)
  13. canvas.pack()
  14.  
  15. cell_size = img_size // 4
  16. font = ImageFont.truetype("arial.ttf", 16)
  17.  
  18. steps = [i for i in range(0, 256, 50)]
  19. rgb_tuples = list(itertools.product(steps, repeat=3))
  20. rgb_tuples = sorted(rgb_tuples, key=lambda x: sum(x))
  21. base = len(rgb_tuples)
  22.  
  23. default_image = [i for i in range(4) for _ in range(4)]
  24. default_image.pop(-1)
  25.  
  26. def next_highest_combo(perm, base=10):
  27.     size = len(perm)
  28.     max_num = max(perm)
  29.  
  30.     if perm == sorted(perm)[::-1]:
  31.         perm.reverse()
  32.         for i in range(len(perm) - 1, -1, -1):
  33.             if perm[i] < max_num:
  34.                 perm[i] += 1
  35.                 for j in range(i + 1, len(perm) - 1):
  36.                     perm[j] = perm[i]
  37.                 return perm
  38.  
  39.     i = size - 2
  40.     while i >= 0 and perm[i] >= perm[i + 1]:
  41.         i -= 1
  42.     if i == -1:
  43.         if max(perm) == base:
  44.             return None
  45.         return default_image + [max(perm) + 1]
  46.         # return [0] * (size - 1) + [max(perm) + 1]
  47.  
  48.     j = size - 1
  49.     while perm[j] <= perm[i]:
  50.         j -= 1
  51.  
  52.     perm[i], perm[j] = perm[j], perm[i]
  53.     perm = perm[:i+1] + perm[i+1:][::-1]
  54.  
  55.     return perm
  56.  
  57. def draw_grid(flat_list):
  58.     fg_image = Image.new("RGBA", (img_size, img_size), (0, 0, 0, 0))
  59.     fg_draw = ImageDraw.Draw(fg_image)
  60.     bg_image = Image.new("RGB", (img_size, img_size), "white")
  61.     bg_draw = ImageDraw.Draw(bg_image)
  62.  
  63.     for i in range(4):
  64.         for j in range(4):
  65.             index = i * 4 + j
  66.             r, g, b = flat_list[index]
  67.             x0, y0 = j * cell_size, i * cell_size
  68.             x1, y1 = x0 + cell_size, y0 + cell_size
  69.            
  70.             bg_draw.rectangle([x0, y0, x1, y1], fill=(r, g, b))
  71.  
  72.             text = f"{r}\n{g}\n{b}"
  73.             fg_draw.text((x0 + 20, y0 + 10), text, fill="white", font=font)
  74.  
  75.     blurred_bg = bg_image.filter(ImageFilter.GaussianBlur(radius=25))
  76.     final_image = Image.alpha_composite(blurred_bg.convert("RGBA"), fg_image)
  77.  
  78.     return final_image
  79.  
  80. perm = [0] * 25
  81. while True:
  82.     perm = next_highest_combo(perm, base)
  83.     if not perm:
  84.         break
  85.     grid = [rgb_tuples[i] for i in perm]
  86.     grid_image = draw_grid(grid)
  87.  
  88.     photo_image = ImageTk.PhotoImage(grid_image)
  89.     canvas.create_image(0, 0, anchor="nw", image=photo_image)
  90.     canvas.update()
  91.  
  92. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement