Advertisement
here2share

# tk_native_grayscale_ani.py

Jun 5th, 2023
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. # tk_native_grayscale_ani.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw
  5. from math import sin, cos, pi
  6. import random
  7.  
  8. ww = 384
  9. hh = 384
  10. cx, cy = ww//2, hh//2
  11.  
  12. root = Tk()
  13. root.title("native_grayscale_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.04 * min(img.size)
  27.  
  28. # set the minimum and maximum colors of the water
  29. black = (0, 0, 0)
  30. gray = (128, 128, 128)
  31. white = (255, 255, 255)
  32.  
  33. # create a custom color palette for interpolation
  34. palette = []
  35. for i in range(0,256):
  36.     j = i % 32
  37.     if j > 20:
  38.         color = white
  39.     elif j > 18:
  40.         color = gray
  41.     else:
  42.         color = black
  43.     palette.append(color)
  44. Lc = len(palette)
  45.  
  46. canvas = Canvas(root, width=ww, height=hh, bg='white')
  47. canvas.pack(side=LEFT, fill=BOTH, expand=True)
  48.  
  49. def display():
  50.     tkimg = ImageTk.PhotoImage(draw['source'])
  51.     canvas.create_image((cx, cy), image=tkimg)
  52.     canvas.update()
  53.  
  54. sz = 16
  55. c = 0
  56. xy = range(0, 512, sz)
  57. for y in xy:
  58.     c = (c + 5) % 11
  59.     for x in xy:
  60.         color = colors.pop(c)
  61.         colors.append(color)
  62.         draw[1].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  63.         c = (c + 5) % 11
  64. draw['source'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  65. source = {}
  66. target = {}
  67. for y in range(hh):
  68.     for x in range(ww):
  69.         source[x,y] = draw['source'].getpixel((x, y))
  70.        
  71. def wave_transition(x, y, alpha):
  72.     source_color = source[x, y]
  73.     target_color = draw['target'].getpixel((x, y))
  74.  
  75.     r = int((1 - alpha) * source_color[0] + alpha * target_color[0])
  76.     g = int((1 - alpha) * source_color[1] + alpha * target_color[1])
  77.     b = int((1 - alpha) * source_color[2] + alpha * target_color[2])
  78.    
  79.     # apply the custom color palette
  80.     color = int(r + g + b) % Lc
  81.     draw['source'].putpixel((x, y), palette[color])
  82.  
  83.  
  84. def waves():
  85.     alpha = 0.1
  86.     while alpha < 0.8:
  87.         for y in range(hh):
  88.             for x in range(ww):
  89.                 wave_transition(x, y, alpha)
  90.         alpha += 0.1
  91.         display()
  92.  
  93.     for y in range(hh):
  94.         for x in range(ww):
  95.             target_color = draw['target'].getpixel((x, y))
  96.             wave_transition(x, y, alpha=0.9)
  97.             source[x, y] = target_color # fastest solution I can think of
  98.     display()
  99.    
  100. while 1:
  101.     for y in xy:
  102.         c = (c + 1) % 11
  103.         for x in xy:
  104.             color = colors.pop(c)
  105.             colors.append(color)
  106.             draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  107.             c = (c + 1) % 11
  108.     draw['target'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  109.  
  110.     waves()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement