Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_spacebarbarian.py ZZZ
- import tkinter as tk
- from PIL import Image, ImageTk, ImageFilter, ImageDraw, ImageEnhance
- import random
- def shuffle_pixels():
- random.shuffle(xy)
- img.putdata(xy)
- def kernelize(e=None):
- global kernel_2, img, blur, blur2, contrast, alpha, zoom_factor
- kernel_2 = ImageFilter.Kernel(
- size=(3, 3),
- kernel=[random.uniform(-9.0, 9.0) for _ in range(9)],
- scale=2,
- offset=0
- )
- shuffle_pixels()
- img = img.filter(ImageFilter.GaussianBlur(radius=random.uniform(0.1, 5.0)))
- blur = random.uniform(1.0, 2.0)
- blur2 = random.uniform(1.0, 2.0)
- contrast = random.uniform(0.1, 2.0)
- zoom_factor = random.uniform(1.05, 1.5)
- alpha = random.uniform(0.100, 0.999)
- def create_lenia():
- global img, canvas_image, blur, blur2
- wave = 1
- zoom_factor = 1.05
- blur = 1.5
- blur2 = 1.5
- contrast = 1.5
- rotation_angle = 0
- alpha = 0.7
- while True:
- img = img.filter(kernel_1)
- img = img.filter(ImageFilter.GaussianBlur(radius=blur))
- img = img.rotate(rotation_angle, resample=Image.BICUBIC, expand=True)
- rotation_angle += 0.0001
- new_size = int(size * zoom_factor)
- img = img.resize((new_size, new_size), Image.LANCZOS)
- left = (new_size - size) // 2
- top = (new_size - size) // 2
- right = (new_size + size) // 2
- bottom = (new_size + size) // 2
- img = img.crop((left, top, right, bottom))
- img = ImageEnhance.Contrast(img).enhance(contrast)
- if wave % 2:
- img = img.filter(ImageFilter.GaussianBlur(radius=blur2))
- rgb = img.filter(kernel_2)
- rgb = Image.blend(img, rgb, alpha=alpha) # because sometimes kernel_2 fails
- canvas_image = ImageTk.PhotoImage(rgb)
- canvas.create_image(0, 0, anchor=tk.NW, image=canvas_image)
- root.update_idletasks()
- root.update()
- wave = not wave
- size = 600
- root = tk.Tk()
- canvas = tk.Canvas(root, width=size, height=size)
- canvas.pack()
- img = Image.new("RGB", (size, size))
- draw = ImageDraw.Draw(img)
- kernel_1 = ImageFilter.Kernel(
- size=(3, 3),
- kernel=[
- 0.05, 0.2, 0.05,
- 0.2, -1.0, 0.2,
- 0.05, 0.2, 0.05
- ],
- scale=None,
- offset=0
- )
- cc = {}
- for x in range(size):
- for y in range(size):
- cc[x,y] = 0
- xy = []
- x2 = 256.0 / size
- y2 = 256.0 / size
- for y in range(size):
- b = int(y * y2)
- for x in range(size):
- a = int(x * x2)
- c = cc[a, b]
- xy.append((a, b, c))
- cc[a, b] = c + 1
- kernelize()
- root.bind("<space>", kernelize)
- create_lenia()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement