here2share

# tk_cache_zoom_demo.py

Sep 28th, 2025
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. # tk_cache_zoom_demo.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFont
  5. import math
  6. WW, HH = 640, 640
  7. cx, cy = WW // 2, HH // 2
  8. root = tk.Tk()
  9. root.geometry('+0+0')
  10. canvas = tk.Canvas(root, width=WW, height=HH)
  11. canvas.pack()
  12. grid_colors = {
  13.     (0, 0): (255, 255, 255), (0, 1): (255, 255, 0), (0, 2): (0, 255, 0),
  14.     (1, 0): (255, 165, 0),   (1, 1): (128, 128, 128), (1, 2): (0, 0, 255),
  15.     (2, 0): (255, 0, 0),     (2, 1): (128, 0, 128),   (2, 2): (0, 0, 0)
  16. }
  17. small_img = Image.new('RGB', (3, 3))
  18. for y in range(3):
  19.     for x in range(3):
  20.         small_img.putpixel((x, y), grid_colors[(x, y)])
  21. base_img = small_img.resize((WW, HH))
  22. colors = list(base_img.getdata())
  23. active_pixels = set()
  24. def get_degrees(x, y):
  25.     dx = x - cx
  26.     dy = y - cy
  27.     degrees = math.degrees(math.atan2(-dy, dx))
  28.     if degrees < 0:
  29.         degrees += 360
  30.     return degrees
  31. def get_distance_from_center(x, y):
  32.     dx = x - cx
  33.     dy = y - cy
  34.     distance = math.sqrt(dx * dx + dy * dy)
  35.     return distance
  36.    
  37. zoom_cache = {}
  38. for y in range(HH):
  39.     for x in range(WW):
  40.         degrees = get_degrees(x, y)
  41.         angle = math.radians(degrees)
  42.         distance = get_distance_from_center(x, y) + 10
  43.         nx = cx + distance * math.cos(angle)
  44.         ny = cy - distance * math.sin(angle)
  45.         if 0 <= nx < WW and 0 <= ny < HH:
  46.             zoom_cache[(x, y)] = {1: (int(round(nx)), int(round(ny)))}
  47.             active_pixels.add((x, y))
  48. while 1:
  49.     photo = ImageTk.PhotoImage(base_img)
  50.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  51.     root.update()
  52.    
  53.     new_img = Image.new('RGB', (WW, HH), (0, 0, 0))
  54.     updated_pixels = set()
  55.     for (x, y) in active_pixels:
  56.         try:
  57.             src_x, src_y = zoom_cache[(x, y)][1]
  58.             color = base_img.getpixel((x, y))
  59.             new_img.putpixel((src_x, src_y), color)
  60.             updated_pixels.add((src_x, src_y))
  61.         except:
  62.             0
  63.     base_img = new_img
  64.     active_pixels = updated_pixels
Advertisement
Add Comment
Please, Sign In to add comment