Advertisement
here2share

# 512x512_rgb_gel_ctrl.py

Jan 10th, 2024 (edited)
1,442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. # 512x512_rgb_gel_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. colors = []
  18. t = [z for z in range(0, 256, 15)]
  19. for r in t:
  20.     for g in t:
  21.         for b in t:
  22.             colors += [(r, g, b, 255)]
  23. Lc = len(colors)
  24.  
  25. def generate(event=None):
  26.     for size in layers:
  27.         interpolate(size)
  28.     draw()
  29.  
  30. def interpolate(size):
  31.     global idx
  32.     idx += 1
  33.     img = Image.new("RGBA", (size, size))
  34.     pixels = img.load()
  35.     for y in range(size):
  36.         for x in range(size):
  37.             color = colors.pop(idx**3)
  38.             idx = (idx + 1) % 7
  39.             pixels[x, y] = color
  40.             colors.append(color)
  41.     img = img.resize((ww, hh))
  42.     layers[size] = img
  43.  
  44. def draw(event=None):
  45.     buffer = Image.new("RGBA", (ww, hh), (255, 255, 255, 255))
  46.     for size, layer_image in layers.items():
  47.         if size != 4:
  48.             alpha = float(opacity_slider[size].get()) / 800.0
  49.         else:
  50.             alpha = 1    
  51.         buffer = Image.blend(buffer, layer_image, alpha)
  52.        
  53.     buffer = buffer.point(lambda x: x // 25 * 25)
  54.    
  55.     photo = ImageTk.PhotoImage(buffer)
  56.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  57.     canvas.image = photo
  58.  
  59. opacity_label = tk.Toplevel(root)
  60. opacity_title = tk.Label(opacity_label, text="Press Spacebar To Randomize")
  61. opacity_title.pack()
  62. opacity_slider = {}
  63. layers = {}
  64.  
  65. idx = 0
  66. size = 8
  67. for i in (512, 512, 96):
  68.     opacity_slider[size] = tk.Scale(opacity_label, from_=512, to=0, orient=tk.VERTICAL, label=size, command=draw)
  69.     opacity_slider[size].pack(side=tk.LEFT, fill='both', expand=True)
  70.     opacity_slider[size].set(i)
  71.     interpolate(size)
  72.     size *= 2
  73.  
  74. canvas.update()
  75. t = opacity_label.winfo_width()
  76. opacity_label.geometry("%dx300+%d+%d" %(t, 50, 50))
  77. for t in (root, opacity_label):
  78.     t.bind("<space>", generate)
  79. opacity_label.wm_transient(root)
  80.  
  81. draw()
  82. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement