Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_colorbrightness_art.py
- import tkinter as tk
- from PIL import Image, ImageDraw, ImageTk, ImageFilter
- import random
- ww = 600
- hh = 600
- root = tk.Tk()
- root.geometry("600x600+0+0")
- canvas = tk.Canvas(root, width=ww, height=hh)
- canvas.pack()
- total_colors = ww * hh
- step = 3.605
- ttt = []
- i = 255
- while i > -1:
- ttt += [int(i)]
- i -= step
- colors = []
- def list_colors():
- rrr = ggg = bbb = ttt[:]
- while 1:
- for r in rrr:
- for g in ggg:
- for b in bbb:
- colors.append((r, g, b))
- if len(colors) == total_colors:
- return
- bbb = bbb[::-1]
- ggg = ggg[::-1]
- rrr = rrr[::-1]
- list_colors()
- colors = sorted(colors, key=lambda x: sum(x))
- print(len(set(colors)))
- def color_transfer(event):
- img = new_img = Image.new('RGB', (ww, hh), (255, 255, 255))
- draw = ImageDraw.Draw(img)
- for _ in range(50):
- x = random.randint(0, ww)
- y = random.randint(0, hh)
- radius = random.randint(20, 50)
- color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
- draw.ellipse((x-radius, y-radius, x+radius, y+radius), fill=color)
- img = img.filter(ImageFilter.GaussianBlur(30))
- img0 = []
- for y in range(hh):
- for x in range(ww):
- pixel = img.getpixel((x, y))
- img0.append(((x, y), pixel))
- img0_sorted = sorted(img0, key=lambda x: sum((x[1][0], x[1][1], x[1][2])))
- for ((x, y), _), color in zip(img0_sorted, colors):
- new_img.putpixel((x, y), color)
- draw_image(new_img)
- def draw_image(img):
- tk_img = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=tk.NW, image=tk_img)
- root.mainloop()
- root.bind("<space>", color_transfer)
- color_transfer(0)
Add Comment
Please, Sign In to add comment