Advertisement
here2share

# tk_rgb_blur_blend_at16_ani.py

Jun 6th, 2023
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. # tk_rgb_blur_blend_at16_ani.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw
  5. from math import sin, cos, pi
  6. import copy
  7.  
  8. ww = 512
  9. hh = 512
  10. cx, cy = ww//2, hh//2
  11.  
  12. root = Tk()
  13. root.title("tk_rgb_blur_blend_at16_ani")
  14. root.geometry("%dx%d+0+0"%(ww,hh))
  15.  
  16. def rgb2hex(r,g,b):
  17.     return '#%02X%02X%02X'%(r,g,b)
  18.  
  19. rgb = range(0, 256, 50)
  20. colors = [rgb2hex(r, g, b) for r in rgb for g in rgb for b in rgb]
  21.  
  22. img = Image.new('RGB', (ww, hh), (0, 0, 0))
  23. draw = {}
  24. for k in (0, 1, 2, 3):
  25.     draw[k] = ImageDraw.Draw(img)
  26. blur_radius = 0.02 * min(img.size)
  27.  
  28. canvas = Canvas(root, width=ww, height=hh, bg='white')
  29. canvas.pack(side=LEFT, fill=BOTH, expand=True)
  30.  
  31. def display():
  32.     tkimg = ImageTk.PhotoImage(draw['image'])
  33.     canvas.create_image((cx, cy), image=tkimg)
  34.     canvas.update()
  35.  
  36. sz = 16
  37. c = 0
  38. xy = range(0, 512, sz)
  39. for y in xy:
  40.     c = (c + 3) % 11
  41.     for x in xy:
  42.         color = colors.pop(c)
  43.         colors.append(color)
  44.         draw[1].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  45.         c = (c + 1) % 11
  46. draw['source'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  47. source = {}
  48. target = {}
  49. for y in range(hh):
  50.     for x in range(ww):
  51.         source[x,y] = draw['source'].getpixel((x, y))
  52.        
  53. o255 = [i for i in range(256)]
  54. o255 = o255[1:-1] + o255[::-1]
  55. Lc = len(o255)
  56.  
  57. def waves():
  58.     for i in range(0, 100):
  59.         alpha = i / 100.0
  60.         draw['image'] = Image.blend(draw['source'], draw['target'], alpha)
  61.         display()
  62.     draw['source'] = copy.deepcopy(draw['target'])
  63.    
  64. while 1:
  65.     for y in xy:
  66.         c = (c + 1) % 11
  67.         for x in xy:
  68.             color = colors.pop(c)
  69.             colors.append(color)
  70.             draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  71.             c = (c + 1) % 11
  72.     draw['target'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  73.  
  74.     waves()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement