Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_512x512_blur_ctrl.py
- import tkinter as tk
- import random
- from PIL import Image, ImageTk, ImageFilter
- ww = 512
- hh = 512
- root = tk.Tk()
- root.title("tk_512x512_blur_ctrl")
- root.geometry("%dx%d+%d+%d" %(ww, hh, 10, 10))
- canvas = tk.Canvas(root, width=ww, height=hh)
- canvas.pack()
- def generate(event=None):
- for size in layers:
- interpolate(size)
- draw()
- iii = []
- def interpolate(size):
- img = Image.new("RGBA", (size, size))
- pixels = img.load()
- grays = the_grays[:]
- for y in range(size):
- for x in range(size):
- random.shuffle(grays)
- gray = grays.pop()[0]
- pixels[x, y] = (gray, gray, gray, 255)
- iii.append(1)
- img = img.resize((ww, hh))
- layers[size] = img
- def contour_image(image):
- gray_image = image.convert("L")
- threshold = 160 # Adjust threshold as needed
- mask = gray_image.point(lambda x: 255 if x > 230 else x // 10 * 10)
- dilated_mask = mask.filter(ImageFilter.MaxFilter(9))
- red_image = Image.new('RGB', image.size, color='red')
- result_image = Image.composite(image, red_image, dilated_mask)
- return result_image
- def draw(event=None):
- buffer = Image.new("RGBA", (ww, hh), (255, 255, 255, 255))
- for size, layer_image in layers.items():
- if size != sizes[0]:
- alpha = float(opacity_slider[size].get()) / 2056.0
- else:
- alpha = 1
- buffer = Image.blend(buffer, layer_image, alpha)
- contoured_image = contour_image(buffer)
- photo = ImageTk.PhotoImage(contoured_image)
- canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- canvas.image = photo
- opacity_label = tk.Toplevel(root)
- opacity_title = tk.Label(opacity_label, text="Press Spacebar To Randomize")
- opacity_title.pack()
- opacity_slider = {}
- layers = {}
- the_grays = [(255, 255, 255, 255) for i in range(700)]
- the_grays += [(0, 0, 0, 255) for i in range(700)]
- size = 8
- sizes = []
- sizes += [size]
- interpolate(size)
- while size + 1 < 32:
- size += 7
- sizes += [size]
- opacity_slider[size] = tk.Scale(opacity_label, from_=600, to=0, orient=tk.VERTICAL, label=size, command=draw)
- opacity_slider[size].pack(side=tk.LEFT, fill='both', expand=True)
- interpolate(size)
- print(len(iii))
- opacity_slider[sizes[1]].set(600)
- opacity_slider[sizes[2]].set(320)
- canvas.update()
- t = opacity_label.winfo_width()
- opacity_label.geometry("%dx300+%d+%d" %(t, 50, 50))
- for t in (root, opacity_label):
- t.bind("<space>", generate)
- opacity_label.wm_transient(root)
- draw()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement