Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_cache_zoom_demo.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageDraw, ImageFont
- import math
- WW, HH = 640, 640
- cx, cy = WW // 2, HH // 2
- root = tk.Tk()
- root.geometry('+0+0')
- canvas = tk.Canvas(root, width=WW, height=HH)
- canvas.pack()
- grid_colors = {
- (0, 0): (255, 255, 255), (0, 1): (255, 255, 0), (0, 2): (0, 255, 0),
- (1, 0): (255, 165, 0), (1, 1): (128, 128, 128), (1, 2): (0, 0, 255),
- (2, 0): (255, 0, 0), (2, 1): (128, 0, 128), (2, 2): (0, 0, 0)
- }
- small_img = Image.new('RGB', (3, 3))
- for y in range(3):
- for x in range(3):
- small_img.putpixel((x, y), grid_colors[(x, y)])
- base_img = small_img.resize((WW, HH))
- colors = list(base_img.getdata())
- active_pixels = set()
- def get_degrees(x, y):
- dx = x - cx
- dy = y - cy
- degrees = math.degrees(math.atan2(-dy, dx))
- if degrees < 0:
- degrees += 360
- return degrees
- def get_distance_from_center(x, y):
- dx = x - cx
- dy = y - cy
- distance = math.sqrt(dx * dx + dy * dy)
- return distance
- zoom_cache = {}
- for y in range(HH):
- for x in range(WW):
- degrees = get_degrees(x, y)
- angle = math.radians(degrees)
- distance = get_distance_from_center(x, y) + 10
- nx = cx + distance * math.cos(angle)
- ny = cy - distance * math.sin(angle)
- if 0 <= nx < WW and 0 <= ny < HH:
- zoom_cache[(x, y)] = {1: (int(round(nx)), int(round(ny)))}
- active_pixels.add((x, y))
- while 1:
- photo = ImageTk.PhotoImage(base_img)
- canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- root.update()
- new_img = Image.new('RGB', (WW, HH), (0, 0, 0))
- updated_pixels = set()
- for (x, y) in active_pixels:
- try:
- src_x, src_y = zoom_cache[(x, y)][1]
- color = base_img.getpixel((x, y))
- new_img.putpixel((src_x, src_y), color)
- updated_pixels.add((src_x, src_y))
- except:
- 0
- base_img = new_img
- active_pixels = updated_pixels
Advertisement
Add Comment
Please, Sign In to add comment