Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_random_infinite_alpha_zoom.py
- import tkinter as tk
- from PIL import Image, ImageTk
- import random
- ww = 400
- hh = 400
- root = tk.Tk()
- root.title("tk_random_infinite_zoom")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = tk.Canvas(root, bg='white', width=ww, height=hh)
- canvas.pack()
- cx, cy = ww//2, hh//2
- fg_img = Image.new('RGBA', (ww, hh), (0, 0, 0, 0))
- bg_img = Image.new('RGBA', (ww, hh), (255, 255, 255, 255)) # change to RGBA to allow for additive blending
- rgb = [i for i in range(0, 256, 5)]
- colors = [(r, g, b, int(255*0.2)) for r in rgb for g in rgb for b in rgb] * 5
- random.shuffle(colors)
- size = ww * hh
- rgb = colors[:size]
- bg_img.putdata(rgb)
- # set initial zoom level and size
- zoom = 25.0
- while True:
- random.shuffle(colors)
- rgb = colors[:size]
- fg_img.putdata(rgb)
- # add foreground image over the background image and update bg_img
- bg_img = Image.alpha_composite(bg_img, fg_img).convert('RGBA') # change to additive blending by removing convert to RGB
- # crop parameters to zoom into the center
- bg_img = bg_img.crop((zoom, zoom, ww - zoom, hh - zoom))
- bg_img = bg_img.resize((ww, hh), resample=Image.LANCZOS)
- # convert image to Tkinter PhotoImage
- tkimg = ImageTk.PhotoImage(bg_img)
- # display image on canvas
- canvas.create_image((cx, cy), image=tkimg)
- canvas.update()
Advertisement
Advertisement