Advertisement
here2share

# tk_buffer_zoom_art.py

Jan 10th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. # tk_buffer_zoom_art.py # just another experiment
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageOps, ImageFilter
  5. import math
  6.  
  7. WW, HH = 600, 600
  8.  
  9. root = tk.Tk()
  10. canvas = tk.Canvas(root, width=WW, height=HH)
  11. root.geometry("%dx%d+10+10" % (WW, HH))
  12. canvas.pack()
  13.  
  14. grid_colors = {
  15.     (0, 0): (255, 255, 255),  # White
  16.     (0, 1): (255, 255, 0),    # Yellow
  17.     (0, 2): (0, 255, 0),      # Green
  18.     (1, 0): (255, 165, 0),    # Orange
  19.     (1, 1): (128, 128, 128),  # Gray (midpoint)
  20.     (1, 2): (0, 0, 255),      # Blue
  21.     (2, 0): (255, 0, 0),      # Red
  22.     (2, 1): (128, 0, 128),    # Purple
  23.     (2, 2): (0, 0, 0)         # Black
  24. }
  25.  
  26. def interpolate_color(color1, color2, factor):
  27.     return tuple(int(color1[i] + (color2[i] - color1[i]) * factor) for i in range(3))
  28.  
  29. def generate_gradient_image(width, height, grid_colors):
  30.     image = Image.new("RGB", (width, height))
  31.     for i in range(width):
  32.         for j in range(height):
  33.             x = i / (width - 1) * 2
  34.             y = j / (height - 1) * 2
  35.             x0, y0 = int(x), int(y)
  36.             x1, y1 = min(x0 + 1, 2), min(y0 + 1, 2)
  37.             fx, fy = x - x0, y - y0
  38.  
  39.             color_top = interpolate_color(grid_colors[(x0, y0)], grid_colors[(x1, y0)], fx)
  40.             color_bottom = interpolate_color(grid_colors[(x0, y1)], grid_colors[(x1, y1)], fx)
  41.             color = interpolate_color(color_top, color_bottom, fy)
  42.  
  43.             image.putpixel((i, j), color)
  44.     return image
  45.  
  46.  
  47. print('creating buffer image...')
  48. buffer_image = generate_gradient_image(WW, HH, grid_colors)
  49. display_image = buffer_image.copy()
  50.  
  51. def reverse_overflow(value):
  52.     return value % 255
  53.  
  54. lookup_table = [reverse_overflow(v) for v in range(511)]
  55.  
  56. zoom = 50
  57. new_width = int(WW + zoom)
  58. new_height = int(HH + zoom)
  59. left = (new_width - WW) // 2
  60. top = (new_height - HH) // 2
  61. right = left + WW
  62. bottom = top + HH
  63.    
  64. def blend_images():
  65.     r1, g1, b1 = buffer_image.split()
  66.     r2, g2, b2 = display_image.split()
  67.  
  68.     r = Image.eval(r1, lambda x: lookup_table[x + r2.getpixel((0, 0))])
  69.     g = Image.eval(g1, lambda x: lookup_table[x + g2.getpixel((0, 0))])
  70.     b = Image.eval(b1, lambda x: lookup_table[x + b2.getpixel((0, 0))])
  71.  
  72.     return Image.merge("RGB", (r, g, b))
  73.    
  74. def plot(img):
  75.     photo = ImageTk.PhotoImage(img)
  76.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  77.     canvas.update()
  78.    
  79. def set_alpha(alpha):
  80.     return Image.new("L", patch_image.size, int(alpha))
  81.  
  82. while True:
  83.     plot(display_image)
  84.  
  85.     display_image = display_image.resize((new_width, new_height), Image.ANTIALIAS)
  86.     display_image = display_image.crop((left, top, right, bottom))
  87.     display_image = display_image.filter(ImageFilter.GaussianBlur(radius=6))
  88.  
  89.     patch_image = blend_images()
  90.  
  91.     alpha_channel = set_alpha(8)
  92.     patch_image.putalpha(alpha_channel)
  93.  
  94.     display_image.paste(patch_image, (0, 0), patch_image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement