Advertisement
here2share

# tk_512x512_blur_ctrl.py

Dec 19th, 2023
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. # tk_512x512_blur_ctrl.py
  2.  
  3. import tkinter as tk
  4. import random
  5. from PIL import Image, ImageTk, ImageFilter
  6.  
  7. ww = 512
  8. hh = 512
  9.  
  10. root = tk.Tk()
  11. root.title("tk_512x512_blur_ctrl")
  12. root.geometry("%dx%d+%d+%d" %(ww, hh, 10, 10))
  13.  
  14. canvas = tk.Canvas(root, width=ww, height=hh)
  15. canvas.pack()
  16.  
  17. def generate(event=None):
  18.     for size in layers:
  19.         interpolate(size)
  20.     draw()
  21.  
  22. iii = []
  23. def interpolate(size):
  24.     img = Image.new("RGBA", (size, size))
  25.     pixels = img.load()
  26.     grays = the_grays[:]
  27.     for y in range(size):
  28.         for x in range(size):
  29.             random.shuffle(grays)
  30.             gray = grays.pop()[0]
  31.             pixels[x, y] = (gray, gray, gray, 255)
  32.             iii.append(1)
  33.     img = img.resize((ww, hh))
  34.     layers[size] = img
  35.  
  36. def contour_image(image):
  37.     gray_image = image.convert("L")
  38.     threshold = 160 # Adjust threshold as needed
  39.     mask = gray_image.point(lambda x: 255 if x > 230 else x // 10 * 10)
  40.     dilated_mask = mask.filter(ImageFilter.MaxFilter(9))
  41.     red_image = Image.new('RGB', image.size, color='red')
  42.     result_image = Image.composite(image, red_image, dilated_mask)
  43.     return result_image
  44.  
  45. def draw(event=None):
  46.     buffer = Image.new("RGBA", (ww, hh), (255, 255, 255, 255))
  47.     for size, layer_image in layers.items():
  48.         if size != sizes[0]:
  49.             alpha = float(opacity_slider[size].get()) / 2056.0
  50.         else:
  51.             alpha = 1    
  52.         buffer = Image.blend(buffer, layer_image, alpha)
  53.    
  54.     contoured_image = contour_image(buffer)
  55.     photo = ImageTk.PhotoImage(contoured_image)
  56.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  57.     canvas.image = photo
  58.  
  59. opacity_label = tk.Toplevel(root)
  60. opacity_title = tk.Label(opacity_label, text="Press Spacebar To Randomize")
  61. opacity_title.pack()
  62. opacity_slider = {}
  63. layers = {}
  64.  
  65. the_grays = [(255, 255, 255, 255) for i in range(700)]
  66. the_grays += [(0, 0, 0, 255) for i in range(700)]
  67.  
  68. size = 8
  69. sizes = []
  70. sizes += [size]
  71. interpolate(size)
  72. while size + 1 < 32:
  73.     size += 7
  74.     sizes += [size]
  75.     opacity_slider[size] = tk.Scale(opacity_label, from_=600, to=0, orient=tk.VERTICAL, label=size, command=draw)
  76.     opacity_slider[size].pack(side=tk.LEFT, fill='both', expand=True)
  77.     interpolate(size)
  78.  
  79. print(len(iii))
  80.  
  81. opacity_slider[sizes[1]].set(600)
  82. opacity_slider[sizes[2]].set(320)
  83.  
  84. canvas.update()
  85. t = opacity_label.winfo_width()
  86. opacity_label.geometry("%dx300+%d+%d" %(t, 50, 50))
  87. for t in (root, opacity_label):
  88.     t.bind("<space>", generate)
  89. opacity_label.wm_transient(root)
  90.  
  91. draw()
  92. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement