Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 512x512_rgb_gradient_ctrl_3.py
- import tkinter as tk
- import random
- from PIL import Image, ImageTk, ImageFilter
- ww = 512
- hh = 512
- root = tk.Tk()
- root.title("512x512_rgb_gradient_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()
- 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()) / 2056.0
- else:
- alpha = 1
- buffer = Image.blend(buffer, layer_image, alpha)
- contour_layer = buffer.point(lambda x: 0 if x % 30 < 15 else 255)
- buffer = Image.blend(buffer, contour_layer, 0.05)
- photo = ImageTk.PhotoImage(buffer)
- 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 = {}
- size = 4
- interpolate(size)
- while size + 6 < 32:
- size += 7
- opacity_slider[size] = tk.Scale(opacity_label, from_=512, to=0, orient=tk.VERTICAL, label=size, command=draw)
- opacity_slider[size].pack(side=tk.LEFT, fill='both', expand=True)
- opacity_slider[size].set(5)
- interpolate(size)
- 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