Advertisement
here2share

# tk_random_infinite_alpha_zoom.py

May 20th, 2023
1,026
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. # tk_random_infinite_alpha_zoom.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk
  5. import random
  6.  
  7. ww = 400
  8. hh = 400
  9.  
  10. root = tk.Tk()
  11. root.title("tk_random_infinite_zoom")
  12. root.geometry("%dx%d+0+0"%(ww,hh))
  13. canvas = tk.Canvas(root, bg='white', width=ww, height=hh)
  14. canvas.pack()
  15.  
  16. cx, cy = ww//2, hh//2
  17.  
  18. fg_img = Image.new('RGBA', (ww, hh), (0, 0, 0, 0))
  19. bg_img = Image.new('RGBA', (ww, hh), (255, 255, 255, 255)) # change to RGBA to allow for additive blending
  20.  
  21. rgb = [i for i in range(0, 256, 5)]
  22. colors = [(r, g, b, int(255*0.2)) for r in rgb for g in rgb for b in rgb] * 5
  23. random.shuffle(colors)
  24. size = ww * hh
  25. rgb = colors[:size]
  26. bg_img.putdata(rgb)
  27.  
  28. # set initial zoom level and size
  29. zoom = 25.0
  30.  
  31. while True:
  32.     random.shuffle(colors)
  33.     rgb = colors[:size]
  34.     fg_img.putdata(rgb)
  35.    
  36.     # add foreground image over the background image and update bg_img
  37.     bg_img = Image.alpha_composite(bg_img, fg_img).convert('RGBA') # change to additive blending by removing convert to RGB
  38.  
  39.     # crop parameters to zoom into the center
  40.     bg_img = bg_img.crop((zoom, zoom, ww - zoom, hh - zoom))
  41.     bg_img = bg_img.resize((ww, hh), resample=Image.LANCZOS)
  42.  
  43.     # convert image to Tkinter PhotoImage
  44.     tkimg = ImageTk.PhotoImage(bg_img)
  45.  
  46.     # display image on canvas
  47.     canvas.create_image((cx, cy), image=tkimg)
  48.     canvas.update()
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement