Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # image_editor.py -- ZZZ looking to add a variant slider... maybe having 0 equals neutral sharpness while 500 equals maximum blur... or... one screen length slider to search through every smooth color combination. The randomness is just to test the code for now.
- import tkinter as tk
- import random
- from PIL import Image, ImageTk, ImageFilter
- import time
- ww = 500
- hh = 500
- root = tk.Tk()
- root.title("Variant Interpolate")
- root.geometry("%dx%d+%d+%d" %(ww, hh, 10, 10))
- canvas = tk.Canvas(root, width=ww, height=hh)
- canvas.pack()
- def up_arrow(event):
- global y
- y = max(0, y-10)
- move_topwin()
- def down_arrow(event):
- global y
- y = min(hh-10, y+10)
- move_topwin()
- def left_arrow(event):
- global x
- x = max(0, x-10)
- move_topwin()
- def right_arrow(event):
- global x
- x = min(ww-10, x+10)
- move_topwin()
- def update_color(event=None):
- red = red_slider.get()
- green = green_slider.get()
- blue = blue_slider.get()
- pixels[x // 10, y // 10] = (red, green, blue)
- interpolate()
- def on_click(event):
- global x, y
- x, y = event.x, event.y
- move_topwin()
- def toggle_circle_visibility(event=None):
- global circle_visible
- circle_visible = not circle_visible
- def move_topwin():
- x0 = y0 = 35
- if x > ww - 230:
- x0 = -230
- x0 = root.winfo_x() + x0 + x
- y0 = root.winfo_y() + y0 + min(y, hh - 150)
- color_window.geometry(f"+{x0}+{y0}")
- red, green, blue = pixels[x // 10, y // 10]
- red_slider.set(red)
- green_slider.set(green)
- blue_slider.set(blue)
- canvas.bind("<Button-1>", on_click)
- def interpolate():
- canvas.delete('all')
- resized_image = canvas_image.resize((ww, hh), resample=Image.BICUBIC)
- blurred_image = resized_image.filter(ImageFilter.GaussianBlur(3.2))
- photo = ImageTk.PhotoImage(blurred_image)
- canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- canvas.image = photo
- def xyfocus():
- global iii
- canvas.delete("circle")
- if circle_visible:
- x0 = x // 10 * 10
- y0 = y // 10 * 10
- c = grayscale[iii]
- outline_color = f"#{c:02x}{c:02x}{c:02x}"
- canvas.create_oval(x0 - 20, y0 - 20, x0 + 30, y0 + 30,
- fill='', outline=outline_color, width=3, tags="circle")
- iii = (iii + 1) % Lgray
- root.after(1, xyfocus)
- canvas_image = Image.new("RGB", (50, 50))
- pixels = canvas_image.load()
- for y in range(50):
- for x in range(50):
- red = random.randint(0, 255)
- green = random.randint(0, 255)
- blue = random.randint(0, 255)
- pixels[x, y] = (red, green, blue)
- interpolate()
- iii = range(0, 256, 2)
- grayscale = []
- for i in iii:
- grayscale += [(i)]
- for i in iii:
- grayscale += [(255-i)]
- Lgray = len(grayscale)
- color_window = tk.Toplevel(root)
- red_slider = tk.Scale(color_window, from_=255, to=0, orient=tk.VERTICAL, label="R", command=update_color)
- red_slider.pack(side=tk.LEFT)
- green_slider = tk.Scale(color_window, from_=255, to=0, orient=tk.VERTICAL, label="G", command=update_color)
- green_slider.pack(side=tk.LEFT)
- blue_slider = tk.Scale(color_window, from_=255, to=0, orient=tk.VERTICAL, label="B", command=update_color)
- blue_slider.pack(side=tk.LEFT)
- color_window.geometry("+%d+%d" %(100, 100))
- color_window.wm_transient(root)
- x = y = 0
- circle_visible = 1
- red, green, blue = pixels[x // 10, y // 10]
- red_slider.set(red)
- green_slider.set(green)
- blue_slider.set(blue)
- for t in (root, color_window):
- t.bind("<Up>", up_arrow)
- t.bind("<Down>", down_arrow)
- t.bind("<Left>", left_arrow)
- t.bind("<Right>", right_arrow)
- t.bind("<space>", toggle_circle_visibility)
- def adjust_slider(event, slider):
- slider.set(max(0, min(255, slider.get() + (1 if event.keysym in 'qwe' else -1))))
- update_color()
- bindings = {
- "<KeyPress-q>": red_slider,
- "<KeyPress-a>": red_slider,
- "<KeyPress-w>": green_slider,
- "<KeyPress-s>": green_slider,
- "<KeyPress-e>": blue_slider,
- "<KeyPress-d>": blue_slider
- }
- for key, slider in bindings.items():
- color_window.bind(key, lambda event, slider=slider: adjust_slider(event, slider))
- iii = 0
- xyfocus()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement