Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_buffer_zoom_art.py # just another experiment
- import tkinter as tk
- from PIL import Image, ImageTk, ImageOps, ImageFilter
- import math
- WW, HH = 600, 600
- root = tk.Tk()
- canvas = tk.Canvas(root, width=WW, height=HH)
- root.geometry("%dx%d+10+10" % (WW, HH))
- canvas.pack()
- 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
- }
- def interpolate_color(color1, color2, factor):
- return tuple(int(color1[i] + (color2[i] - color1[i]) * factor) for i in range(3))
- def generate_gradient_image(width, height, grid_colors):
- image = Image.new("RGB", (width, height))
- for i in range(width):
- for j in range(height):
- x = i / (width - 1) * 2
- y = j / (height - 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)
- return image
- print('creating buffer image...')
- buffer_image = generate_gradient_image(WW, HH, grid_colors)
- display_image = buffer_image.copy()
- def reverse_overflow(value):
- return value % 255
- lookup_table = [reverse_overflow(v) for v in range(511)]
- zoom = 50
- new_width = int(WW + zoom)
- new_height = int(HH + zoom)
- left = (new_width - WW) // 2
- top = (new_height - HH) // 2
- right = left + WW
- bottom = top + HH
- def blend_images():
- r1, g1, b1 = buffer_image.split()
- r2, g2, b2 = display_image.split()
- r = Image.eval(r1, lambda x: lookup_table[x + r2.getpixel((0, 0))])
- g = Image.eval(g1, lambda x: lookup_table[x + g2.getpixel((0, 0))])
- b = Image.eval(b1, lambda x: lookup_table[x + b2.getpixel((0, 0))])
- return Image.merge("RGB", (r, g, b))
- def plot(img):
- photo = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- canvas.update()
- def set_alpha(alpha):
- return Image.new("L", patch_image.size, int(alpha))
- while True:
- plot(display_image)
- display_image = display_image.resize((new_width, new_height), Image.ANTIALIAS)
- display_image = display_image.crop((left, top, right, bottom))
- display_image = display_image.filter(ImageFilter.GaussianBlur(radius=6))
- patch_image = blend_images()
- alpha_channel = set_alpha(8)
- patch_image.putalpha(alpha_channel)
- display_image.paste(patch_image, (0, 0), patch_image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement