Advertisement
here2share

# tk_random_infinite_zoom.py

May 20th, 2023
1,410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. # tk_random_infinite_zoom.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  5. import random
  6. import math
  7.  
  8. ww = 300
  9. hh = 300
  10.  
  11. root = tk.Tk()
  12. root.title("tk_random_infinite_zoom")
  13. root.geometry("%dx%d+0+0"%(ww,hh))
  14. canvas = tk.Canvas(root, bg='white', width=ww, height=hh)
  15. canvas.pack()
  16.  
  17. cx, cy = ww//2, hh//2
  18.  
  19. img = Image.new('RGB', (ww, hh), "white")
  20.  
  21. zoom = 1.9
  22. rnd = 10
  23. w2 = int(ww*0.25)
  24. h2 = int(hh*0.25)
  25. while 1:
  26.     for y0 in range(w2, ww-w2, 2):
  27.         for x0 in range(h2, hh-h2, 2):
  28.             for (x, y) in ((x0, y0), (x0+1, y0), (x0, y0+1)):
  29.                 r, g, b = img.getpixel((x, y))
  30.                 r = max(0, min(255, r + random.randint(-rnd, rnd)))
  31.                 g = max(0, min(255, g + random.randint(-rnd, rnd)))
  32.                 b = max(0, min(255, b + random.randint(-rnd, rnd)))
  33.                 img.putpixel((x, y), (r, g, b))
  34.  
  35.     # crop parameters to zoom into the center
  36.     img = img.crop((zoom*3, zoom*3, ww-zoom*3, hh-zoom*3))
  37.     img = img.resize((ww, hh), resample=Image.LANCZOS)
  38.  
  39.     tkimg = ImageTk.PhotoImage(img)
  40.     canvas.create_image((cx, cy), image=tkimg)
  41.  
  42.     canvas.update()
  43.     canvas.scale('all', cx, cy, zoom, zoom)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement