Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_combo_gradients.py
- import tkinter as tk
- from PIL import Image, ImageTk
- from itertools import combinations
- WW, HH = 1200, 120
- root = tk.Tk()
- root.geometry('+0+0')
- root.title("tk_combo_gradients (Click image to toggle resample)")
- container = tk.Frame(root)
- container.pack(fill=tk.BOTH, expand=True)
- canvas = tk.Canvas(container, width=WW, height=HH)
- canvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
- h_scrollbar = tk.Scrollbar(container, orient=tk.HORIZONTAL, command=canvas.xview)
- h_scrollbar.pack(side=tk.BOTTOM, fill=tk.X)
- canvas.configure(xscrollcommand=h_scrollbar.set)
- base = {
- (0, 0): (255, 255, 255), (0, 1): (255, 255, 0), (0, 2): (0, 255, 0),
- (1, 0): (255, 128, 0), (1, 1): (128, 128, 128), (1, 2): (0, 0, 255),
- (2, 0): (255, 0, 0), (2, 1): (128, 0, 128), (2, 2): (0, 0, 0)
- }
- colors = [(255, 192, 203), (165, 42, 42), ( 0, 255, 255)]
- colors = list(base.values()) + colors
- pairs = list(combinations(colors, 2))
- combos = []
- used_pairs = set()
- current_pair = pairs.pop(0)
- combos.extend(current_pair)
- used_pairs.add(current_pair)
- while pairs:
- last_color = combos[-1]
- next_pair = None
- for p in pairs:
- if p not in used_pairs:
- if p[0] == last_color:
- next_pair = p
- rev = False
- break
- elif p[1] == last_color:
- next_pair = p
- rev = True
- break
- if next_pair:
- pairs.remove(next_pair)
- used_pairs.add(next_pair)
- if rev:
- combos.append(next_pair[0])
- else:
- combos.append(next_pair[1])
- else:
- p = pairs.pop(0)
- used_pairs.add(p)
- combos.extend(p)
- gradient_width = 50
- total_width = len(combos) * gradient_width
- total_height = 100
- base_img = Image.new("RGB", (len(combos), total_height))
- for i, color in enumerate(combos):
- for y in range(total_height):
- base_img.putpixel((i, y), color)
- # State tracking for resampling mode
- resample_modes = [
- ("LANCZOS", Image.LANCZOS),
- ("NEAREST", Image.NEAREST)
- ]
- current_mode_index = 0
- photo = None
- image_item = None
- def update_image():
- global photo, image_item, current_mode_index
- mode_name, mode_val = resample_modes[current_mode_index]
- # Resize using current mode
- resized_img = base_img.resize((total_width, total_height), resample=mode_val)
- photo = ImageTk.PhotoImage(resized_img)
- if image_item is None:
- image_item = canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- else:
- canvas.itemconfig(image_item, image=photo)
- root.title(f"tk_combo_gradients - Resample: {mode_name} (Click image to toggle)")
- def toggle_resample(event):
- global current_mode_index
- current_mode_index = (current_mode_index + 1) % len(resample_modes)
- update_image()
- update_image()
- canvas.config(scrollregion=(0, 0, total_width, total_height))
- canvas.bind("<Button-1>", toggle_resample)
- def on_mouse_wheel(event):
- canvas.xview_scroll(int(-1 * (event.delta / 120)), "units")
- canvas.bind_all("<MouseWheel>", on_mouse_wheel)
- root.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment