Advertisement
here2share

# 512x512_rgb_gradient_ctrl.py

Dec 17th, 2023
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. # 512x512_rgb_gradient_ctrl.py
  2.  
  3. import tkinter as tk
  4. import random
  5. from PIL import Image, ImageTk, ImageFilter
  6.  
  7. ww = 512
  8. hh = 512
  9.  
  10. root = tk.Tk()
  11. root.title("512x512_image_generator")
  12. root.geometry("%dx%d+%d+%d" %(ww, hh, 10, 10))
  13.  
  14. canvas = tk.Canvas(root, width=ww, height=hh)
  15. canvas.pack()
  16.  
  17. def generate(event=None):
  18.     for size in layers:
  19.         interpolate(size)
  20.     draw()
  21.  
  22. def interpolate(size):
  23.     img = Image.new("RGBA", (size, size))
  24.     pixels = img.load()
  25.     for y in range(size):
  26.         for x in range(size):
  27.             red = random.randint(0, 255)
  28.             green = random.randint(0, 255)
  29.             blue = random.randint(0, 255)
  30.             pixels[x, y] = (red, green, blue, 255)
  31.     img = img.resize((ww, hh))
  32.     layers[size] = img
  33.  
  34. def draw(event=None):
  35.     buffer = Image.new("RGBA", (ww, hh), (255, 255, 255, 255))
  36.     for size, layer_image in layers.items():
  37.         if size != 4:
  38.             alpha = float(opacity_slider[size].get()) / 2056.0
  39.         else:
  40.             alpha = 1    
  41.         buffer = Image.blend(buffer, layer_image, alpha)
  42.    
  43.     photo = ImageTk.PhotoImage(buffer)
  44.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  45.     canvas.image = photo
  46.  
  47. opacity_label = tk.Toplevel(root)
  48. opacity_title = tk.Label(opacity_label, text="Press Spacebar To Randomize")
  49. opacity_title.pack()
  50. opacity_slider = {}
  51. layers = {}
  52.  
  53. size = 4
  54. interpolate(size)
  55. while size - 1 < 512 / 4:
  56.     size *= 2
  57.     opacity_slider[size] = tk.Scale(opacity_label, from_=512, to=0, orient=tk.VERTICAL, label=size, command=draw)
  58.     opacity_slider[size].pack(side=tk.LEFT, fill='both', expand=True)
  59.     opacity_slider[size].set(5)
  60.     interpolate(size)
  61.  
  62. canvas.update()
  63. t = opacity_label.winfo_width()
  64. opacity_label.geometry("%dx300+%d+%d" %(t, 50, 50))
  65. for t in (root, opacity_label):
  66.     t.bind("<space>", generate)
  67. opacity_label.wm_transient(root)
  68.  
  69. draw()
  70. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement