Advertisement
here2share

# image_editor.py

Dec 9th, 2023
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. # 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.
  2.  
  3. import tkinter as tk
  4. import random
  5. from PIL import Image, ImageTk, ImageFilter
  6. import time
  7.  
  8. ww = 500
  9. hh = 500
  10.  
  11. root = tk.Tk()
  12. root.title("Variant Interpolate")
  13. root.geometry("%dx%d+%d+%d" %(ww, hh, 10, 10))
  14.  
  15. canvas = tk.Canvas(root, width=ww, height=hh)
  16. canvas.pack()
  17.  
  18. def up_arrow(event):
  19.     global y
  20.     y = max(0, y-10)
  21.     move_topwin()
  22. def down_arrow(event):
  23.     global y
  24.     y = min(hh-10, y+10)
  25.     move_topwin()
  26.  
  27. def left_arrow(event):
  28.     global x
  29.     x = max(0, x-10)
  30.     move_topwin()
  31.    
  32. def right_arrow(event):
  33.     global x
  34.     x = min(ww-10, x+10)
  35.     move_topwin()
  36.  
  37. def update_color(event=None):
  38.     red = red_slider.get()
  39.     green = green_slider.get()
  40.     blue = blue_slider.get()
  41.     pixels[x // 10, y // 10] = (red, green, blue)
  42.     interpolate()
  43.    
  44. def on_click(event):
  45.     global x, y
  46.     x, y = event.x, event.y
  47.     move_topwin()
  48.    
  49. def toggle_circle_visibility(event=None):
  50.     global circle_visible
  51.     circle_visible = not circle_visible
  52.    
  53. def move_topwin():
  54.     x0 = y0 = 35
  55.     if x > ww - 230:
  56.         x0 = -230
  57.     x0 = root.winfo_x() + x0 + x
  58.     y0 = root.winfo_y() + y0 + min(y, hh - 150)
  59.     color_window.geometry(f"+{x0}+{y0}")
  60.    
  61.     red, green, blue = pixels[x // 10, y // 10]
  62.     red_slider.set(red)
  63.     green_slider.set(green)
  64.     blue_slider.set(blue)
  65.  
  66. canvas.bind("<Button-1>", on_click)
  67.  
  68. def interpolate():
  69.     canvas.delete('all')
  70.  
  71.     resized_image = canvas_image.resize((ww, hh), resample=Image.BICUBIC)
  72.  
  73.     blurred_image = resized_image.filter(ImageFilter.GaussianBlur(3.2))
  74.  
  75.     photo = ImageTk.PhotoImage(blurred_image)
  76.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  77.     canvas.image = photo
  78.    
  79. def xyfocus():
  80.     global iii
  81.     canvas.delete("circle")
  82.     if circle_visible:
  83.         x0 = x // 10 * 10
  84.         y0 = y // 10 * 10
  85.         c = grayscale[iii]
  86.         outline_color = f"#{c:02x}{c:02x}{c:02x}"
  87.         canvas.create_oval(x0 - 20, y0 - 20, x0 + 30, y0 + 30,
  88.                 fill='', outline=outline_color, width=3, tags="circle")
  89.         iii = (iii + 1) % Lgray
  90.     root.after(1, xyfocus)
  91.  
  92. canvas_image = Image.new("RGB", (50, 50))
  93. pixels = canvas_image.load()
  94. for y in range(50):
  95.     for x in range(50):
  96.         red = random.randint(0, 255)
  97.         green = random.randint(0, 255)
  98.         blue = random.randint(0, 255)
  99.         pixels[x, y] = (red, green, blue)
  100. interpolate()
  101.  
  102. iii = range(0, 256, 2)
  103. grayscale = []
  104. for i in iii:
  105.     grayscale += [(i)]
  106. for i in iii:
  107.     grayscale += [(255-i)]
  108. Lgray = len(grayscale)
  109.  
  110. color_window = tk.Toplevel(root)
  111. red_slider = tk.Scale(color_window, from_=255, to=0, orient=tk.VERTICAL, label="R", command=update_color)
  112. red_slider.pack(side=tk.LEFT)
  113.  
  114. green_slider = tk.Scale(color_window, from_=255, to=0, orient=tk.VERTICAL, label="G", command=update_color)
  115. green_slider.pack(side=tk.LEFT)
  116.  
  117. blue_slider = tk.Scale(color_window, from_=255, to=0, orient=tk.VERTICAL, label="B", command=update_color)
  118. blue_slider.pack(side=tk.LEFT)
  119.  
  120. color_window.geometry("+%d+%d" %(100, 100))
  121. color_window.wm_transient(root)
  122.  
  123. x = y = 0
  124. circle_visible = 1
  125.  
  126. red, green, blue = pixels[x // 10, y // 10]
  127. red_slider.set(red)
  128. green_slider.set(green)
  129. blue_slider.set(blue)
  130.  
  131. for t in (root, color_window):
  132.     t.bind("<Up>", up_arrow)
  133.     t.bind("<Down>", down_arrow)
  134.     t.bind("<Left>", left_arrow)
  135.     t.bind("<Right>", right_arrow)
  136.  
  137.     t.bind("<space>", toggle_circle_visibility)
  138.    
  139. def adjust_slider(event, slider):
  140.     slider.set(max(0, min(255, slider.get() + (1 if event.keysym in 'qwe' else -1))))
  141.     update_color()
  142.  
  143. bindings = {
  144.     "<KeyPress-q>": red_slider,
  145.     "<KeyPress-a>": red_slider,
  146.     "<KeyPress-w>": green_slider,
  147.     "<KeyPress-s>": green_slider,
  148.     "<KeyPress-e>": blue_slider,
  149.     "<KeyPress-d>": blue_slider
  150. }
  151.  
  152. for key, slider in bindings.items():
  153.     color_window.bind(key, lambda event, slider=slider: adjust_slider(event, slider))
  154.  
  155. iii = 0
  156. xyfocus()
  157.  
  158. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement