Advertisement
here2share

# 512x512_rgb_gradient_ctrl_3.py

Dec 20th, 2023
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. # 512x512_rgb_gradient_ctrl_3.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_rgb_gradient_ctrl")
  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.     contour_layer = buffer.point(lambda x: 0 if x % 30 < 15 else 255)
  44.     buffer = Image.blend(buffer, contour_layer, 0.05)
  45.    
  46.     photo = ImageTk.PhotoImage(buffer)
  47.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  48.     canvas.image = photo
  49.  
  50. opacity_label = tk.Toplevel(root)
  51. opacity_title = tk.Label(opacity_label, text="Press Spacebar To Randomize")
  52. opacity_title.pack()
  53. opacity_slider = {}
  54. layers = {}
  55.  
  56. size = 4
  57. interpolate(size)
  58. while size + 6 < 32:
  59.     size += 7
  60.     opacity_slider[size] = tk.Scale(opacity_label, from_=512, to=0, orient=tk.VERTICAL, label=size, command=draw)
  61.     opacity_slider[size].pack(side=tk.LEFT, fill='both', expand=True)
  62.     opacity_slider[size].set(5)
  63.     interpolate(size)
  64.  
  65. canvas.update()
  66. t = opacity_label.winfo_width()
  67. opacity_label.geometry("%dx300+%d+%d" %(t, 50, 50))
  68. for t in (root, opacity_label):
  69.     t.bind("<space>", generate)
  70. opacity_label.wm_transient(root)
  71.  
  72. draw()
  73. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement