Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_4x2_Color_Gradient.py
- from tkinter import *
- from PIL import Image, ImageTk
- import math
- root = Tk()
- root.title("# Tk_4x2_Color_Gradient")
- ww = 1200
- hh = 600
- root.geometry("%dx%d+-10+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack()
- colors = [(0, 0, 0), (255, 0, 0), (255, 165, 0), (255, 255, 0),
- (75, 0, 130), (0, 0, 255), (0, 128, 0), (255, 255, 255)]
- rx = 4 / ww
- ry = 2 / hh
- def bicubic(c00, c10, c01, c11, x, y):
- w00 = (1 - x) * (1 - y)
- w10 = x * (1 - y)
- w01 = (1 - x) * y
- w11 = x * y
- r = int(w00 * c00[0] + w10 * c10[0] + w01 * c01[0] + w11 * c11[0])
- g = int(w00 * c00[1] + w10 * c10[1] + w01 * c01[1] + w11 * c11[1])
- b = int(w00 * c00[2] + w10 * c10[2] + w01 * c01[2] + w11 * c11[2])
- return (r, g, b)
- def transition_color():
- x_index = int(x // (ww // 4))
- y_index = int(y // (hh // 2)) * 4
- c00 = colors[x_index + y_index]
- if mode == "bicubic":
- c10 = colors[(x_index + 1) + y_index]
- c01 = colors[x_index + (y_index + 4)]
- c11 = colors[(x_index + 1) + (y_index + 4)]
- x_ratio = x * rx
- y_ratio = y * ry
- x_fraction = x_ratio - x_index
- y_fraction = y_ratio - y_index
- return bicubic(c00, c10, c01, c11, x_fraction, y_fraction)
- return c00
- def toggle_mode(event):
- global toggle, mode
- mode = "solid" if mode == "bicubic" else "bicubic"
- toggle = 1
- root.bind("<space>", toggle_mode)
- rgb = [(128, 128, 128)] * (ww * hh)
- mode = "bicubic"
- toggle = 1
- img = Image.new('RGB', (ww, hh))
- while 1:
- if toggle:
- if mode == "bicubic":
- for y in range(hh // 2):
- for x in range(int(ww * 0.75)):
- rgb[x + y * ww] = transition_color()
- else:
- for y in range(hh):
- for x in range(ww):
- rgb[x + y * ww] = transition_color()
- img.putdata(rgb)
- if mode == "bicubic":
- img.putdata(rgb)
- img = img.crop((0, 0, int(ww * 0.75), int(hh * 0.5)))
- img = img.resize((ww, hh))
- imgTk = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=NW, image=imgTk)
- toggle = 0
- root.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement