Advertisement
here2share

# Tk_4x2_Color_Gradient_Alive.py

Aug 11th, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. # Tk_4x2_Color_Gradient_Alive.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk
  5. import math
  6. import random
  7. import time
  8.  
  9. root = Tk()
  10. root.title("# Tk_4x2_Color_Gradient_Alive")
  11. ww = 1200
  12. hh = 600
  13. zz = 40
  14. w0 = int(ww / zz)
  15. h0 = int(hh / zz)
  16. root.geometry("%dx%d+-10+0"%(ww,hh))
  17. canvas = Canvas(root, width=ww, height=hh)
  18. canvas.pack()
  19.  
  20. colors = [(0, 0, 0), (255, 0, 0), (255, 165, 0), (255, 255, 0),
  21. (75, 0, 130), (0, 0, 255), (0, 128, 0), (255, 255, 255)]
  22.  
  23. rx = (4 - 1) / ww
  24. ry = (2 - 1) / hh
  25.  
  26. def bicubic(c00, c10, c01, c11, x, y):
  27.     w00 = (1 - x) * (1 - y)
  28.     w10 = x * (1 - y)
  29.     w01 = (1 - x) * y
  30.     w11 = x * y
  31.  
  32.     r = int(w00 * c00[0] + w10 * c10[0] + w01 * c01[0] + w11 * c11[0])
  33.     g = int(w00 * c00[1] + w10 * c10[1] + w01 * c01[1] + w11 * c11[1])
  34.     b = int(w00 * c00[2] + w10 * c10[2] + w01 * c01[2] + w11 * c11[2])
  35.  
  36.     return (r, g, b)
  37.  
  38. def transition_color(x, y):
  39.     x *= zz
  40.     y *= zz
  41.     x_ratio = x * rx
  42.     y_ratio = y * ry
  43.     x_index = int(x_ratio)
  44.     y_index = int(y_ratio) * 4
  45.     c00 = colors[x_index + y_index]
  46.     c10 = colors[(x_index + 1) + y_index]
  47.     c01 = colors[x_index + (y_index + 4)]
  48.     c11 = colors[(x_index + 1) + (y_index + 4)]
  49.    
  50.     x_fraction = x_ratio - x_index
  51.     y_fraction = y_ratio - y_index
  52.  
  53.     return bicubic(c00, c10, c01, c11, x_fraction, y_fraction)
  54.  
  55. def color_difference(color1, color2):
  56.     return sum(abs(a - b) for a, b in zip(color1, color2))
  57.  
  58. def make_alive():
  59.     current_color = rgb[xy2rgb[x, y]]
  60.     min_difference = float('inf')
  61.     xx, yy = x, y
  62.  
  63.     for x0, y0 in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
  64.         x2, y2 = x0 + x, y0 + y
  65.         ttt = [((x2 + j * x0 if x0 else x + i), (y2 + j * y0 if y0 else y + i)) for i in (-1, 0, 1) for j in [1, 2, 3]]
  66.         for dx, dy in ttt:
  67.             if min(dx, dy) > -1:
  68.                 try:
  69.                     neighbor_color = rgb[xy2rgb[x0, y0]]
  70.                     rgb[xy2rgb[x2, y2]] # dblchk
  71.                     difference = color_difference(current_color, neighbor_color)
  72.                     if difference < min_difference:
  73.                         min_difference = difference
  74.                         xx, yy = x2, y2
  75.                 except:
  76.                     0
  77.     rgb[xy2rgb[x, y]], rgb[xy2rgb[xx, yy]] = rgb[xy2rgb[xx, yy]], rgb[xy2rgb[x, y]]
  78.     return xx, yy
  79.  
  80. def toggle_mode(event):
  81.     random.shuffle(rgb)
  82. root.bind("<space>", toggle_mode)
  83.  
  84. img = Image.new('RGB', (ww, hh))
  85. img2 = Image.new('RGB', (w0, h0))
  86.  
  87. rgb = [(128, 128, 128)] * (w0 * h0)
  88. xy2rgb = {}
  89. XYo = []
  90.  
  91. for y in range(h0):
  92.     for x in range(w0):
  93.         rgb[x + y * w0] = transition_color(x, y)
  94.         xy2rgb[x, y] = len(xy2rgb)
  95.         XYo += [(x, y)]
  96.    
  97. while 1:
  98.     swapped = {}
  99.     random.shuffle(XYo)
  100.     for (x, y) in XYo:
  101.         x, y = make_alive()
  102.         img2.putdata(rgb)
  103.         img = img2.crop((0, 0, w0, h0))
  104.         img = img.resize((ww, hh))
  105.         imgTk = ImageTk.PhotoImage(img)
  106.         canvas.create_image(0, 0, anchor=NW, image=imgTk)
  107.         root.update()
  108.    
  109. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement