Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_slider_art_generator.py
- ### ZZZ todo: try to find a far more efficent way for it to generate the given pattern whereas entering any number will determine it
- from tkinter import Tk, Canvas
- from PIL import Image, ImageTk, ImageDraw, ImageFont, ImageFilter
- import itertools
- img_size = 400
- root = Tk()
- root.title("# tk_slider_art_generator.py")
- canvas = Canvas(root, width=img_size, height=img_size)
- canvas.pack()
- cell_size = img_size // 4
- font = ImageFont.truetype("arial.ttf", 16)
- steps = [i for i in range(0, 256, 50)]
- rgb_tuples = list(itertools.product(steps, repeat=3))
- rgb_tuples = sorted(rgb_tuples, key=lambda x: sum(x))
- base = len(rgb_tuples)
- default_image = [i for i in range(4) for _ in range(4)]
- default_image.pop(-1)
- def next_highest_combo(perm, base=10):
- size = len(perm)
- max_num = max(perm)
- if perm == sorted(perm)[::-1]:
- perm.reverse()
- for i in range(len(perm) - 1, -1, -1):
- if perm[i] < max_num:
- perm[i] += 1
- for j in range(i + 1, len(perm) - 1):
- perm[j] = perm[i]
- return perm
- i = size - 2
- while i >= 0 and perm[i] >= perm[i + 1]:
- i -= 1
- if i == -1:
- if max(perm) == base:
- return None
- return default_image + [max(perm) + 1]
- # return [0] * (size - 1) + [max(perm) + 1]
- j = size - 1
- while perm[j] <= perm[i]:
- j -= 1
- perm[i], perm[j] = perm[j], perm[i]
- perm = perm[:i+1] + perm[i+1:][::-1]
- return perm
- def draw_grid(flat_list):
- fg_image = Image.new("RGBA", (img_size, img_size), (0, 0, 0, 0))
- fg_draw = ImageDraw.Draw(fg_image)
- bg_image = Image.new("RGB", (img_size, img_size), "white")
- bg_draw = ImageDraw.Draw(bg_image)
- for i in range(4):
- for j in range(4):
- index = i * 4 + j
- r, g, b = flat_list[index]
- x0, y0 = j * cell_size, i * cell_size
- x1, y1 = x0 + cell_size, y0 + cell_size
- bg_draw.rectangle([x0, y0, x1, y1], fill=(r, g, b))
- text = f"{r}\n{g}\n{b}"
- fg_draw.text((x0 + 20, y0 + 10), text, fill="white", font=font)
- blurred_bg = bg_image.filter(ImageFilter.GaussianBlur(radius=25))
- final_image = Image.alpha_composite(blurred_bg.convert("RGBA"), fg_image)
- return final_image
- perm = [0] * 25
- while True:
- perm = next_highest_combo(perm, base)
- if not perm:
- break
- grid = [rgb_tuples[i] for i in perm]
- grid_image = draw_grid(grid)
- photo_image = ImageTk.PhotoImage(grid_image)
- canvas.create_image(0, 0, anchor="nw", image=photo_image)
- canvas.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement