Advertisement
here2share

# tk_ink_blotches.py

Dec 21st, 2023
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. # tk_ink_blotches.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("tk_ink_blotches")
  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 apply_blur(image, radius):
  18.     return image.filter(ImageFilter.GaussianBlur(radius))
  19.  
  20. def apply_sharpen(image, factor):
  21.     return image.filter(ImageFilter.UnsharpMask(factor))
  22.  
  23. def generate(event=None):
  24.     for size in layers:
  25.         interpolate(size)
  26.     draw()
  27.  
  28. def interpolate(size):
  29.     img = Image.new("RGBA", (size, size))
  30.     pixels = img.load()
  31.     for y in range(size):
  32.         for x in range(size):
  33.             red = random.randint(0, 255)
  34.             green = random.randint(0, 255)
  35.             blue = random.randint(0, 255)
  36.             pixels[x, y] = (red, green, blue, 255)
  37.     img = img.resize((ww, hh))
  38.     layers[size] = img
  39.  
  40. def draw(event=None):
  41.     buffer = Image.new("RGBA", (ww, hh), (255, 255, 255, 255))
  42.     for size, layer_image in layers.items():
  43.         if size != 4:
  44.             alpha = (float(opacity_slider[size].get()) + 512) / 2056.0
  45.         else:
  46.             alpha = 1
  47.         buffer = Image.blend(buffer, layer_image, alpha)
  48.    
  49.     pixel_data = [(0, 0, 0, 255) if sum(pixel) % 128 < 64 else (255, 255, 255, 255) for pixel in buffer.getdata()]
  50.     contour_layer = Image.new("RGBA", (ww, hh))
  51.     contour_layer.putdata(pixel_data)
  52.    
  53.     contour_layer = apply_blur(contour_layer, 2.5)
  54.     contour_layer = apply_sharpen(contour_layer, 25)
  55.    
  56.     photo = ImageTk.PhotoImage(contour_layer)
  57.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  58.     canvas.image = photo
  59.  
  60. opacity_label = tk.Toplevel(root)
  61. opacity_title = tk.Label(opacity_label, text="Spacebar To Randomize")
  62. opacity_title.pack()
  63. opacity_slider = {}
  64. layers = {}
  65.  
  66. size = 4
  67. interpolate(size)
  68. sizes = []
  69. sizes += [size]
  70. while size + 6 < 18:
  71.     size += 7
  72.     sizes += [size]
  73.     opacity_slider[size] = tk.Scale(opacity_label, from_=512, to=0, orient=tk.VERTICAL, label='', command=draw)
  74.     opacity_slider[size].pack(side=tk.LEFT, fill='both', expand=True)
  75.     opacity_slider[size].set(5)
  76.     interpolate(size)
  77.  
  78. opacity_slider[sizes[1]].set(300)
  79. opacity_slider[sizes[2]].set(300)
  80.  
  81. canvas.update()
  82. t = opacity_label.winfo_width()
  83. opacity_label.geometry("%dx300+%d+%d" %(t+20, 50, 50))
  84. for t in (root, opacity_label):
  85.     t.bind("<space>", generate)
  86. opacity_label.wm_transient(root)
  87.  
  88. draw()
  89. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement