Advertisement
here2share

# tk_oil_spill_ani.py

Jun 5th, 2023
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. # tk_oil_spill_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_oil_spill_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. 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['source'])
  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 + 5) % 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 + 5) % 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 wave_transition(x, y, alpha):
  58.     source_color = source[x, y]
  59.     target_color = draw['target'].getpixel((x, y))
  60.  
  61.     r = int((1 - alpha) * source_color[0] + alpha * target_color[0])
  62.     g = int((1 - alpha) * source_color[1] + alpha * target_color[1])
  63.     b = int((1 - alpha) * source_color[2] + alpha * target_color[2])
  64.    
  65.     i = int(r + g * 255 + b * 255 * 255)
  66.     color = [(i + j) % Lc for j in (r, g, b)]
  67.     draw['source'].putpixel((x, y), tuple(color))
  68.  
  69. def waves():
  70.     alpha = 0.1
  71.     while alpha < 0.8:
  72.         for y in range(hh):
  73.             for x in range(ww):
  74.                 wave_transition(x, y, alpha)
  75.         alpha += 0.1
  76.         display()
  77.  
  78.     for y in range(hh):
  79.         for x in range(ww):
  80.             target_color = draw['target'].getpixel((x, y))
  81.             wave_transition(x, y, alpha=0.9)
  82.             source[x, y] = target_color # fastest solution I can think of
  83.     display()
  84.    
  85. while 1:
  86.     for y in xy:
  87.         c = (c + 1) % 11
  88.         for x in xy:
  89.             color = colors.pop(c)
  90.             colors.append(color)
  91.             draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  92.             c = (c + 1) % 11
  93.     draw['target'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  94.  
  95.     waves()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement