Advertisement
here2share

# tk_zoomify.py

Aug 19th, 2024 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. # tk_zoomify.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk
  5. import math
  6.  
  7. WW, HH = 600, 600
  8. WC, HC = WW // 2, HH // 2
  9.  
  10. root = tk.Tk()
  11. canvas = tk.Canvas(root, width=WW, height=HH)
  12. root.geometry("%dx%d+10+10" % (WW, HH))
  13. canvas.pack()
  14. image = Image.new("RGB", (WW, HH))
  15. canvas.update()
  16.  
  17. def interpolate_color(color1, color2, factor):
  18.     return tuple(int(color1[i] + (color2[i] - color1[i]) * factor) for i in range(3))
  19.  
  20. grid_colors = {
  21.     (0, 0): (255, 255, 255),  # White
  22.     (0, 1): (255, 255, 0),    # Yellow
  23.     (0, 2): (0, 255, 0),      # Green
  24.     (1, 0): (255, 165, 0),    # Orange
  25.     (1, 1): (128, 128, 128),  # Gray (midpoint)
  26.     (1, 2): (0, 0, 255),      # Blue
  27.     (2, 0): (255, 0, 0),      # Red
  28.     (2, 1): (128, 0, 128),    # Purple
  29.     (2, 2): (0, 0, 0)         # Black
  30. }
  31.  
  32. # Interpolate colors for each pixel
  33. for i in range(WW):
  34.     for j in range(HH):
  35.         x = i / (WW - 1) * 2
  36.         y = j / (HH - 1) * 2
  37.         x0, y0 = int(x), int(y)
  38.         x1, y1 = min(x0 + 1, 2), min(y0 + 1, 2)
  39.         fx, fy = x - x0, y - y0
  40.  
  41.         color_top = interpolate_color(grid_colors[(x0, y0)], grid_colors[(x1, y0)], fx)
  42.         color_bottom = interpolate_color(grid_colors[(x0, y1)], grid_colors[(x1, y1)], fx)
  43.         color = interpolate_color(color_top, color_bottom, fy)
  44.  
  45.         image.putpixel((i, j), color)
  46.  
  47. gradient_photo = ImageTk.PhotoImage(image)
  48.  
  49. canvas_image = canvas.create_image(0, 0, anchor=tk.NW, image=gradient_photo)
  50. canvas.update()
  51.  
  52. steps = 3
  53. zz = 250
  54. XY = {(WC - 1, HC - 1), (WC, HC - 1), (WC - 1, HC), (WC, HC)}
  55. for x in range(zz, WW-zz, steps):
  56.     for y in range(zz, HH-zz, steps):
  57.         XY.add((x, y))
  58. XY = tuple(XY)
  59.  
  60. def rgb_cycle(value):
  61.     if value % 2:
  62.         return max(0, value-25)
  63.     else:
  64.         return min(255, value+25)
  65.        
  66. def add_pattern(image):
  67.     pixels = image.load()
  68.     for x, y in XY:
  69.         r, g, b = [rgb_cycle(i) for i in pixels[x, y]]
  70.         pixels[x, y] = (r, g, b)
  71.  
  72.     return image
  73.  
  74. zoom = 20
  75. i = 0
  76. while 1:
  77.     image = image.crop((zoom + i, zoom + i, WW-zoom + i, HH-zoom + i))
  78.     image = image.resize((WW, HH), Image.ANTIALIAS)
  79.     image = add_pattern(image)
  80.     gradient_photo = ImageTk.PhotoImage(image)
  81.     canvas.itemconfig(canvas_image, image=gradient_photo)
  82.     canvas.update()
  83.     i = abs(i - 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement