Advertisement
here2share

# tk_500x500_rgb_gel_ani.py

Jan 11th, 2024 (edited)
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. # tk_500x500_rgb_gel_ani.py
  2.  
  3. import tkinter as tk
  4. import random
  5. from PIL import Image, ImageTk, ImageFilter
  6.  
  7. ww = 500
  8. hh = 500
  9. cx = ww // 2
  10. cy = ww // 2
  11.  
  12. root = tk.Tk()
  13. root.title("tk_500x500_rgb_gel_ani")
  14. root.geometry("%dx%d+%d+%d" %(ww, hh, 10, 10))
  15.  
  16. canvas = tk.Canvas(root, width=ww, height=hh)
  17. canvas.pack()
  18.  
  19. colors = []
  20. t = [z for z in range(0, 256, 15)]
  21. for r in t:
  22.     for g in t:
  23.         for b in t:
  24.             colors += [(r, g, b, 255)]
  25. Lc = len(colors)
  26.  
  27. def generate(event=None):
  28.     for size in layers:
  29.         interpolate(size)
  30.     draw()
  31.  
  32. def interpolate(size):
  33.     global idx
  34.     img = Image.new("RGBA", (size, size))
  35.     pixels = img.load()
  36.     for y in range(size):
  37.         for x in range(size):
  38.             color = colors.pop(idx**3)
  39.             idx = (idx + 1) % 16
  40.             pixels[x, y] = color
  41.             colors.append(color)
  42.     img = img.resize((ww+20, hh+20))
  43.     layers[size] = img
  44.  
  45. def draw(event=None):
  46.     buffer = Image.new("RGBA", (ww+20, hh+20), (255, 255, 255, 255))
  47.     for size, layer_image in layers.items():
  48.         buffer = Image.blend(buffer, layer_image, 400 / 1000.0)
  49.        
  50.     buffer = buffer.point(lambda x: x // 25 * 25)
  51.    
  52.     photo = ImageTk.PhotoImage(buffer)
  53.     canvas.create_image(cx, cy, anchor=tk.CENTER, image=photo)
  54.     canvas.image = photo
  55.     canvas.update()
  56. layers = {}
  57.  
  58. idx = 0
  59. size = 8
  60. for i in (512, 512):
  61.     interpolate(size)
  62.     size *= 2
  63.  
  64. while True:
  65.     generate()
  66.     idx += 1
  67. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement