Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_zoomify.py
- import tkinter as tk
- from PIL import Image, ImageTk
- import math
- WW, HH = 600, 600
- WC, HC = WW // 2, HH // 2
- root = tk.Tk()
- canvas = tk.Canvas(root, width=WW, height=HH)
- root.geometry("%dx%d+10+10" % (WW, HH))
- canvas.pack()
- image = Image.new("RGB", (WW, HH))
- canvas.update()
- def interpolate_color(color1, color2, factor):
- return tuple(int(color1[i] + (color2[i] - color1[i]) * factor) for i in range(3))
- grid_colors = {
- (0, 0): (255, 255, 255), # White
- (0, 1): (255, 255, 0), # Yellow
- (0, 2): (0, 255, 0), # Green
- (1, 0): (255, 165, 0), # Orange
- (1, 1): (128, 128, 128), # Gray (midpoint)
- (1, 2): (0, 0, 255), # Blue
- (2, 0): (255, 0, 0), # Red
- (2, 1): (128, 0, 128), # Purple
- (2, 2): (0, 0, 0) # Black
- }
- # Interpolate colors for each pixel
- for i in range(WW):
- for j in range(HH):
- x = i / (WW - 1) * 2
- y = j / (HH - 1) * 2
- x0, y0 = int(x), int(y)
- x1, y1 = min(x0 + 1, 2), min(y0 + 1, 2)
- fx, fy = x - x0, y - y0
- color_top = interpolate_color(grid_colors[(x0, y0)], grid_colors[(x1, y0)], fx)
- color_bottom = interpolate_color(grid_colors[(x0, y1)], grid_colors[(x1, y1)], fx)
- color = interpolate_color(color_top, color_bottom, fy)
- image.putpixel((i, j), color)
- gradient_photo = ImageTk.PhotoImage(image)
- canvas_image = canvas.create_image(0, 0, anchor=tk.NW, image=gradient_photo)
- canvas.update()
- steps = 3
- zz = 250
- XY = {(WC - 1, HC - 1), (WC, HC - 1), (WC - 1, HC), (WC, HC)}
- for x in range(zz, WW-zz, steps):
- for y in range(zz, HH-zz, steps):
- XY.add((x, y))
- XY = tuple(XY)
- def rgb_cycle(value):
- if value % 2:
- return max(0, value-25)
- else:
- return min(255, value+25)
- def add_pattern(image):
- pixels = image.load()
- for x, y in XY:
- r, g, b = [rgb_cycle(i) for i in pixels[x, y]]
- pixels[x, y] = (r, g, b)
- return image
- zoom = 20
- i = 0
- while 1:
- image = image.crop((zoom + i, zoom + i, WW-zoom + i, HH-zoom + i))
- image = image.resize((WW, HH), Image.ANTIALIAS)
- image = add_pattern(image)
- gradient_photo = ImageTk.PhotoImage(image)
- canvas.itemconfig(canvas_image, image=gradient_photo)
- canvas.update()
- i = abs(i - 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement