Advertisement
here2share

# Tk_4x2_Color_Gradient.py

Aug 10th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. # Tk_4x2_Color_Gradient.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk
  5. import math
  6.  
  7. root = Tk()
  8. root.title("# Tk_4x2_Color_Gradient")
  9. ww = 1200
  10. hh = 600
  11. root.geometry("%dx%d+-10+0"%(ww,hh))
  12. canvas = Canvas(root, width=ww, height=hh)
  13. canvas.pack()
  14.  
  15. colors = [(0, 0, 0), (255, 0, 0), (255, 165, 0), (255, 255, 0),
  16. (75, 0, 130), (0, 0, 255), (0, 128, 0), (255, 255, 255)]
  17.  
  18. rx = 4 / ww
  19. ry = 2 / hh
  20.  
  21. def bicubic(c00, c10, c01, c11, x, y):
  22.     w00 = (1 - x) * (1 - y)
  23.     w10 = x * (1 - y)
  24.     w01 = (1 - x) * y
  25.     w11 = x * y
  26.  
  27.     r = int(w00 * c00[0] + w10 * c10[0] + w01 * c01[0] + w11 * c11[0])
  28.     g = int(w00 * c00[1] + w10 * c10[1] + w01 * c01[1] + w11 * c11[1])
  29.     b = int(w00 * c00[2] + w10 * c10[2] + w01 * c01[2] + w11 * c11[2])
  30.  
  31.     return (r, g, b)
  32.  
  33. def transition_color():
  34.     x_index = int(x // (ww // 4))
  35.     y_index = int(y // (hh // 2)) * 4
  36.     c00 = colors[x_index + y_index]
  37.  
  38.     if mode == "bicubic":
  39.         c10 = colors[(x_index + 1) + y_index]
  40.         c01 = colors[x_index + (y_index + 4)]
  41.         c11 = colors[(x_index + 1) + (y_index + 4)]
  42.         x_ratio = x * rx
  43.         y_ratio = y * ry
  44.         x_fraction = x_ratio - x_index
  45.         y_fraction = y_ratio - y_index
  46.  
  47.         return bicubic(c00, c10, c01, c11, x_fraction, y_fraction)
  48.     return c00
  49.  
  50. def toggle_mode(event):
  51.     global toggle, mode
  52.     mode = "solid" if mode == "bicubic" else "bicubic"
  53.     toggle = 1
  54. root.bind("<space>", toggle_mode)
  55.  
  56. rgb = [(128, 128, 128)] * (ww * hh)
  57. mode = "bicubic"
  58. toggle = 1
  59. img = Image.new('RGB', (ww, hh))
  60.    
  61. while 1:
  62.     if toggle:
  63.         if mode == "bicubic":
  64.             for y in range(hh // 2):
  65.                 for x in range(int(ww * 0.75)):
  66.                     rgb[x + y * ww] = transition_color()
  67.         else:
  68.             for y in range(hh):
  69.                 for x in range(ww):
  70.                     rgb[x + y * ww] = transition_color()
  71.         img.putdata(rgb)
  72.         if mode == "bicubic":
  73.             img.putdata(rgb)
  74.             img = img.crop((0, 0, int(ww * 0.75), int(hh * 0.5)))
  75.             img = img.resize((ww, hh))
  76.         imgTk = ImageTk.PhotoImage(img)
  77.         canvas.create_image(0, 0, anchor=NW, image=imgTk)
  78.         toggle = 0
  79.     root.update()
  80.  
  81. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement