Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_ink_blotches.py
- import tkinter as tk
- import random
- from PIL import Image, ImageTk, ImageFilter
- ww = 512
- hh = 512
- root = tk.Tk()
- root.title("tk_ink_blotches")
- root.geometry("%dx%d+%d+%d" %(ww, hh, 10, 10))
- canvas = tk.Canvas(root, width=ww, height=hh)
- canvas.pack()
- def apply_blur(image, radius):
- return image.filter(ImageFilter.GaussianBlur(radius))
- def apply_sharpen(image, factor):
- return image.filter(ImageFilter.UnsharpMask(factor))
- def generate(event=None):
- for size in layers:
- interpolate(size)
- draw()
- def interpolate(size):
- img = Image.new("RGBA", (size, size))
- pixels = img.load()
- for y in range(size):
- for x in range(size):
- red = random.randint(0, 255)
- green = random.randint(0, 255)
- blue = random.randint(0, 255)
- pixels[x, y] = (red, green, blue, 255)
- img = img.resize((ww, hh))
- layers[size] = img
- def draw(event=None):
- buffer = Image.new("RGBA", (ww, hh), (255, 255, 255, 255))
- for size, layer_image in layers.items():
- if size != 4:
- alpha = (float(opacity_slider[size].get()) + 512) / 2056.0
- else:
- alpha = 1
- buffer = Image.blend(buffer, layer_image, alpha)
- pixel_data = [(0, 0, 0, 255) if sum(pixel) % 128 < 64 else (255, 255, 255, 255) for pixel in buffer.getdata()]
- contour_layer = Image.new("RGBA", (ww, hh))
- contour_layer.putdata(pixel_data)
- contour_layer = apply_blur(contour_layer, 2.5)
- contour_layer = apply_sharpen(contour_layer, 25)
- photo = ImageTk.PhotoImage(contour_layer)
- 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="Spacebar To Randomize")
- opacity_title.pack()
- opacity_slider = {}
- layers = {}
- size = 4
- interpolate(size)
- sizes = []
- sizes += [size]
- while size + 6 < 18:
- size += 7
- sizes += [size]
- opacity_slider[size] = tk.Scale(opacity_label, from_=512, to=0, orient=tk.VERTICAL, label='', command=draw)
- opacity_slider[size].pack(side=tk.LEFT, fill='both', expand=True)
- opacity_slider[size].set(5)
- interpolate(size)
- opacity_slider[sizes[1]].set(300)
- opacity_slider[sizes[2]].set(300)
- canvas.update()
- t = opacity_label.winfo_width()
- opacity_label.geometry("%dx300+%d+%d" %(t+20, 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