Advertisement
here2share

# tk_pseudo_turing_ani.py

Jul 22nd, 2023
1,477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. # tk_pseudo_turing_ani.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw, ImageEnhance
  5. import copy
  6.  
  7. ww = 600
  8. hh = 600
  9. cx, cy = ww//2, hh//2
  10.  
  11. def rgb2hex(r,g,b):
  12.     return '#%02X%02X%02X'%(r,g,b)
  13.  
  14. def display(new_img):
  15.     tkimg = ImageTk.PhotoImage(new_img)
  16.     canvas.create_image(0, 0, anchor=NW, image=tkimg)
  17.     canvas.update()
  18.  
  19. root = Tk()
  20. root.title("tk_pseudo_turing_ani")
  21. root.geometry("%dx%d+10+10"%(ww,hh))
  22. canvas = Canvas(root, width=ww, height=hh, bg='white')
  23. canvas.pack(side=LEFT, fill=BOTH, expand=True)
  24.  
  25. sz = 16
  26. span = ww//sz
  27. c = 0
  28. xy = range(0, sz*span, sz)
  29.        
  30. rgb = range(0, 256, 25)
  31. colors = [rgb2hex(z, z, z) for z in rgb]
  32.  
  33. img = Image.new('RGB', (ww, hh), (0, 0, 0))
  34. draw = {}
  35. draw[0] = ImageDraw.Draw(img)
  36. blur_radius = 0.025 * min(img.size)
  37.  
  38. sz = 30
  39. c = 0
  40. xy = range(0, sz*20, sz)
  41. for y in xy:
  42.     c = (c + 3) % 11
  43.     for x in xy:
  44.         color = colors.pop(c)
  45.         colors.append(color)
  46.         draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  47.         c = (c + 1) % 11
  48. draw['source'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  49.  
  50. def apply_color_threshold(p):
  51.     if p > 128:
  52.         return 0
  53.     else:
  54.         return 255
  55.  
  56. def waves():
  57.     for i in range(0, 100):
  58.         alpha = i / 100.0
  59.         img2 = Image.blend(draw['source'], draw['target'], alpha)
  60.  
  61.         # Apply the color threshold using Image.point for each channel (R, G, B)
  62.         r, g, b = img2.split()
  63.         g = g.point(apply_color_threshold)
  64.         img2 = Image.merge('RGB', (r, g, b))  # Merge the modified channels back into an RGB image
  65.  
  66.         display(img2)
  67.  
  68.     draw['source'] = copy.deepcopy(draw['target'])
  69.    
  70. while 1:
  71.     for y in xy:
  72.         c = (c + 1) % 11
  73.         for x in xy:
  74.             color = colors.pop(c)
  75.             colors.append(color)
  76.             draw[0].rectangle((x, y, x+sz, y+sz), fill=color, outline=color)
  77.             c = (c + 1) % 11
  78.     draw['target'] = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
  79.  
  80.     waves()
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement