-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_picker.py
98 lines (82 loc) · 3.19 KB
/
color_picker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from PIL import Image, ImageDraw, ImageFont
import cv2
from sklearn.cluster import KMeans
import numpy as np
from numpy import linalg as LA
import random
import discord
import requests
import io
client = discord.Client()
Token = 'ENTER YOUR TOKEN'
@client.event
async def on_message(message):
if message.content.startswith('/pic'):
print(message.attachments[0].url)
r = requests.get(message.attachments[0].url)
img = Image.open(io.BytesIO(r.content))
#img_resize = img.resize((500), int(img.height * 500 / img.width))
img.save("image.png")
await message.channel.send("Please wait a moment.")
color_arr = extract_main_color(img_path, 7)
show_tiled_main_color(color_arr)
#draw_random_stripe(color_arr, img_path)
file = discord.File("./image/stripe_image.png", filename="stripe.png")
await message.channel.send(file=file)
def download_img(url, file_name):
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(file_name, 'wb') as f:
f.write(r.content)
def draw_random_stripe(color_arr, img_path):
width = 1024
height = 1024
stripe_color_img = Image.new(
mode='RGB', size=(width, height), color='#333333')
current_height = 0
while current_height < height:
random_index = random.randrange(color_arr.shape[0])
color_hex_str = '#%02x%02x%02x' % tuple(color_arr[random_index])
random_height = random.randrange(5, 70)
color_img = Image.new(
mode='RGB', size=(width, random_height),
color=color_hex_str)
stripe_color_img.paste(
im=color_img,
box=(0, current_height))
current_height += random_height
stripe_color_img.show()
#stripe_color_img.save('./image/stripe_' + img_path)
def show_tiled_main_color(color_arr):
IMG_SIZE = 64
MARGIN = 15
width = IMG_SIZE * color_arr.shape[0] + MARGIN * 2
height = IMG_SIZE + MARGIN * 2
tiled_color_img = Image.new(
mode='RGB', size=(width, height), color='#333333')
for i, rgb_arr in enumerate(color_arr):
color_hex_str = '#%02x%02x%02x' % tuple(rgb_arr)
color_img = Image.new(
mode='RGB', size=(IMG_SIZE, IMG_SIZE),
color=color_hex_str)
tiled_color_img.paste(
im=color_img,
box=(MARGIN + IMG_SIZE * i, MARGIN))
tiled_color_img.show()
tiled_color_img.save('./image/stripe_' + img_path)
def extract_main_color(img_path, color_num):
cv2_img = cv2.imread(img_path)
cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)
cv2_img = cv2_img.reshape(
(cv2_img.shape[0] * cv2_img.shape[1], 3))
cluster = KMeans(n_clusters=color_num)
cluster.fit(X=cv2_img)
cluster_centers_arr = cluster.cluster_centers_.astype(
int, copy=False)
trans_color = cv2_img[0]
cluster_centers_arr = np.array([i for i in cluster_centers_arr if LA.norm(np.array(i - trans_color), 2) > 50])
print("extracted colors array:")
print(cluster_centers_arr)
return cluster_centers_arr
img_path = 'image.png'
client.run('TOKEN')