Advertisement
here2share

# tk_spacebarbarian.py

Jun 16th, 2024
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. # tk_spacebarbarian.py ZZZ
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw, ImageEnhance
  5. import random
  6.  
  7. def shuffle_pixels():
  8.     random.shuffle(xy)
  9.     img.putdata(xy)
  10.  
  11. def kernelize(e=None):
  12.     global kernel_2, img, blur, blur2, contrast, alpha, zoom_factor
  13.     kernel_2 = ImageFilter.Kernel(
  14.         size=(3, 3),
  15.         kernel=[random.uniform(-9.0, 9.0) for _ in range(9)],
  16.         scale=2,
  17.         offset=0
  18.     )
  19.     shuffle_pixels()
  20.     img = img.filter(ImageFilter.GaussianBlur(radius=random.uniform(0.1, 5.0)))
  21.     blur = random.uniform(1.0, 2.0)
  22.     blur2 = random.uniform(1.0, 2.0)
  23.     contrast = random.uniform(0.1, 2.0)
  24.     zoom_factor = random.uniform(1.05, 1.5)
  25.     alpha = random.uniform(0.100, 0.999)
  26.  
  27. def create_lenia():
  28.     global img, canvas_image, blur, blur2
  29.  
  30.     wave = 1
  31.     zoom_factor = 1.05
  32.     blur = 1.5
  33.     blur2 = 1.5
  34.     contrast = 1.5
  35.     rotation_angle = 0
  36.     alpha = 0.7
  37.  
  38.     while True:
  39.         img = img.filter(kernel_1)
  40.         img = img.filter(ImageFilter.GaussianBlur(radius=blur))
  41.         img = img.rotate(rotation_angle, resample=Image.BICUBIC, expand=True)
  42.         rotation_angle += 0.0001
  43.        
  44.         new_size = int(size * zoom_factor)
  45.         img = img.resize((new_size, new_size), Image.LANCZOS)
  46.         left = (new_size - size) // 2
  47.         top = (new_size - size) // 2
  48.         right = (new_size + size) // 2
  49.         bottom = (new_size + size) // 2
  50.         img = img.crop((left, top, right, bottom))
  51.        
  52.         img = ImageEnhance.Contrast(img).enhance(contrast)
  53.        
  54.         if wave % 2:
  55.             img = img.filter(ImageFilter.GaussianBlur(radius=blur2))
  56.             rgb = img.filter(kernel_2)
  57.             rgb = Image.blend(img, rgb, alpha=alpha) # because sometimes kernel_2 fails
  58.            
  59.             canvas_image = ImageTk.PhotoImage(rgb)
  60.             canvas.create_image(0, 0, anchor=tk.NW, image=canvas_image)
  61.             root.update_idletasks()
  62.             root.update()
  63.         wave = not wave
  64.  
  65. size = 600
  66.  
  67. root = tk.Tk()
  68. canvas = tk.Canvas(root, width=size, height=size)
  69. canvas.pack()
  70.  
  71. img = Image.new("RGB", (size, size))
  72. draw = ImageDraw.Draw(img)
  73.  
  74. kernel_1 = ImageFilter.Kernel(
  75.     size=(3, 3),
  76.     kernel=[
  77.         0.05, 0.2, 0.05,
  78.         0.2, -1.0, 0.2,
  79.         0.05, 0.2, 0.05
  80.     ],
  81.     scale=None,
  82.     offset=0
  83. )
  84.  
  85. cc = {}
  86. for x in range(size):
  87.     for y in range(size):
  88.         cc[x,y] = 0
  89.  
  90. xy = []
  91. x2 = 256.0 / size
  92. y2 = 256.0 / size
  93. for y in range(size):
  94.     b = int(y * y2)
  95.     for x in range(size):
  96.         a = int(x * x2)
  97.         c = cc[a, b]
  98.         xy.append((a, b, c))
  99.         cc[a, b] = c + 1
  100.  
  101. kernelize()
  102. root.bind("<space>", kernelize)
  103.  
  104. create_lenia()
  105. root.mainloop()
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement