here2share

# tk_colorbrightness_art.py

Nov 28th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. # tk_colorbrightness_art.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageDraw, ImageTk, ImageFilter
  5. import random
  6.  
  7. ww = 600
  8. hh = 600
  9.  
  10. root = tk.Tk()
  11. root.geometry("600x600+0+0")
  12. canvas = tk.Canvas(root, width=ww, height=hh)
  13. canvas.pack()
  14.  
  15. total_colors = ww * hh
  16. step = 3.605
  17.  
  18. ttt = []
  19. i = 255
  20. while i > -1:
  21.     ttt += [int(i)]
  22.     i -= step
  23.  
  24. colors = []
  25. def list_colors():
  26.     rrr = ggg = bbb = ttt[:]
  27.     while 1:
  28.         for r in rrr:
  29.             for g in ggg:
  30.                 for b in bbb:
  31.                     colors.append((r, g, b))
  32.                     if len(colors) == total_colors:
  33.                         return
  34.                 bbb = bbb[::-1]
  35.             ggg = ggg[::-1]
  36.         rrr = rrr[::-1]
  37.  
  38. list_colors()
  39. colors = sorted(colors, key=lambda x: sum(x))
  40. print(len(set(colors)))
  41.  
  42. def color_transfer(event):
  43.     img = new_img = Image.new('RGB', (ww, hh), (255, 255, 255))
  44.     draw = ImageDraw.Draw(img)
  45.  
  46.     for _ in range(50):
  47.         x = random.randint(0, ww)
  48.         y = random.randint(0, hh)
  49.         radius = random.randint(20, 50)
  50.         color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
  51.         draw.ellipse((x-radius, y-radius, x+radius, y+radius), fill=color)
  52.     img = img.filter(ImageFilter.GaussianBlur(30))
  53.  
  54.     img0 = []
  55.     for y in range(hh):
  56.         for x in range(ww):
  57.             pixel = img.getpixel((x, y))
  58.             img0.append(((x, y), pixel))
  59.            
  60.     img0_sorted = sorted(img0, key=lambda x: sum((x[1][0], x[1][1], x[1][2])))
  61.    
  62.     for ((x, y), _), color in zip(img0_sorted, colors):
  63.         new_img.putpixel((x, y), color)
  64.            
  65.     draw_image(new_img)
  66.  
  67. def draw_image(img):
  68.     tk_img = ImageTk.PhotoImage(img)
  69.     canvas.create_image(0, 0, anchor=tk.NW, image=tk_img)
  70.     root.mainloop()
  71.  
  72. root.bind("<space>", color_transfer)
  73. color_transfer(0)
  74.  
Add Comment
Please, Sign In to add comment