Advertisement
here2share

# tk_water_caustic_ani.py

Jun 5th, 2023 (edited)
1,172
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. # tk_water_caustic_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("tk_water_caustic_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. min_color = (0, 80, 180)
  30. max_color = (0, 210, 250)
  31.  
  32. # create a custom color palette for interpolation
  33. palette = [min_color] * 3
  34. for i in range(0,253,2):
  35.         g = min_color[1] + (max_color[1] - min_color[1]) * i // 245
  36.         b = min_color[2] + (max_color[2] - min_color[2]) * i // 245
  37.         palette.append((0, g, b))
  38. for i in range(253,256):
  39.         palette.append((255, 255, 255))
  40. palette = palette[1:-1]+palette[::-1]
  41.  
  42. Lc = len(palette)
  43.  
  44. canvas = Canvas(root, width=ww, height=hh, bg='white')
  45. canvas.pack(side=LEFT, fill=BOTH, expand=True)
  46.  
  47. def display():
  48.     tkimg = ImageTk.PhotoImage(draw['source'])
  49.     canvas.create_image((cx, cy), image=tkimg)
  50.     canvas.update()
  51.  
  52. sz = 16
  53. c = 0
  54. xy = range(0, 512, sz)
  55. for y in xy:
  56.     for x in xy:
  57.         c = random.randint(0, 50)
  58.         color = colors.pop(c)
  59.         colors.append(color)
  60.         draw[1].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  61. draw['source'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  62. source = {}
  63. target = {}
  64. for y in range(hh):
  65.     for x in range(ww):
  66.         source[x,y] = draw['source'].getpixel((x, y))
  67.        
  68. def wave_transition(x, y, alpha):
  69.     source_color = source[x, y]
  70.     target_color = draw['target'].getpixel((x, y))
  71.  
  72.     r = int((1 - alpha) * source_color[0] + alpha * target_color[0])
  73.     g = int((1 - alpha) * source_color[1] + alpha * target_color[1])
  74.     b = int((1 - alpha) * source_color[2] + alpha * target_color[2])
  75.    
  76.     # apply the custom color palette
  77.     color = int(r + g + b) % Lc
  78.     draw['source'].putpixel((x, y), palette[color])
  79.  
  80.  
  81. def waves():
  82.     alpha = 0.1
  83.     while alpha < 0.8:
  84.         for y in range(hh):
  85.             for x in range(ww):
  86.                 wave_transition(x, y, alpha)
  87.         alpha += 0.1
  88.         display()
  89.  
  90.     for y in range(hh):
  91.         for x in range(ww):
  92.             target_color = draw['target'].getpixel((x, y))
  93.             wave_transition(x, y, alpha=0.9)
  94.             source[x, y] = target_color # fastest solution I can think of
  95.     display()
  96.    
  97. while 1:
  98.     for y in xy:
  99.         c = (c + 1) % 11
  100.         for x in xy:
  101.             color = colors.pop(c)
  102.             colors.append(color)
  103.             draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  104.             c = (c + 1) % 11
  105.     draw['target'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  106.  
  107.     waves()
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement