Advertisement
here2share

# tk_500x500_rgb_cosmic_explosion.py

Jan 21st, 2024 (edited)
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. # tk_500x500_rgb_cosmic_explosion.py
  2.  
  3. import tkinter as tk
  4. import random
  5. from PIL import Image, ImageTk, ImageFilter, ImageDraw
  6.  
  7. ww = 500
  8. hh = 500
  9. cx = ww // 2
  10. cy = ww // 2
  11. p = 60
  12.  
  13. root = tk.Tk()
  14. root.title("# tk_500x500_rgb_cosmic_explosion.py")
  15. root.geometry("%dx%d+%d+%d" %(ww, hh, 10, 10))
  16.  
  17. canvas = tk.Canvas(root, width=ww, height=hh)
  18. canvas.pack()
  19.  
  20. colors = []
  21. t = [z for z in range(0, 256, 15)]
  22. for r in t:
  23.     for g in t:
  24.         for b in t:
  25.             colors += [(r, g, b, 255)]
  26. Lc = len(colors)
  27.  
  28. def crop_center(img):
  29.     w, h = img.size
  30.     target_w, target_h = ww + 2, hh + 2
  31.     left = (w - target_w) // 2
  32.     top = (h - target_h) // 2
  33.     right = left + target_w
  34.     bottom = top + target_h
  35.     return img.crop((left, top, right, bottom))
  36.  
  37. alpha = 255 / 40
  38. black_image = Image.new('RGBA', (ww+p, hh+p), (0, 0, 0))
  39. def zoom():
  40.     global image_a, image_b
  41.     for i in range(1, 45):
  42.         image_a = crop_center(image_a)
  43.         image_a = image_a.resize((ww+p, hh+p), Image.BICUBIC)
  44.         image_a = image_a.filter(ImageFilter.GaussianBlur(1.0))
  45.         image_a = image_a.point(lambda x: x // 15 * 15)
  46.        
  47.         img = Image.blend(image_a.convert("RGBA"), black_image, max(0, i - 30) * 0.05)
  48.         plot(img)
  49.  
  50.     image_a, image_b = image_b, generate_new_image()
  51.  
  52. def generate_new_image():
  53.     global idx
  54.     size = 8
  55.     buffer = 0
  56.     for blur in (400, 400, 400, 400):
  57.         img = Image.new("RGBA", (size, size))
  58.         pixels = img.load()
  59.         for y in range(size):
  60.             for x in range(size):
  61.                 color = colors.pop(idx**3)
  62.                 idx = (idx + 1) % 16
  63.                 pixels[x, y] = color
  64.                 colors.append(color)
  65.         img = img.resize((ww+p, hh+p))
  66.         size *= 2
  67.     return img
  68.  
  69. def plot(buffer):
  70.     rgb_image = buffer.convert('RGB')
  71.     photo = ImageTk.PhotoImage(rgb_image)
  72.     canvas.create_image(cx, cy, anchor=tk.CENTER, image=photo)
  73.     canvas.image = photo
  74.     canvas.update()
  75.    
  76. t=50
  77. mask = Image.new('L', (ww+p, hh+p), 0)
  78. draw = ImageDraw.Draw(mask)
  79. for i in range(0, min(ww+p, hh+p) // 2, 10):
  80.     draw.rectangle([i, i, ww+p - i, hh+p - i], fill=i*255//((min(ww+p, hh+p) // 2)+1))
  81. mask_size = (ww+p, hh+p)
  82.  
  83. idx = 0
  84. image_a, image_b = [generate_new_image() for i in '..']
  85.  
  86. while True:
  87.     zoom()
  88.     idx += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement