-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcharacterize-1.1.py
69 lines (55 loc) · 1.89 KB
/
characterize-1.1.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
# -*- coding: utf-8 -*-
"""
Created on Wed May 1 15:10:17 2019
@author: Shyam Mittal
"""
# these variables can be changed
height = 65
width = 100
character = 'X'
import cv2
import numpy as np
from hex_color_codes1 import codes_dict,codes
from sklearn.neighbors import NearestNeighbors
from colored import fg,attr
rst = attr("reset")
all_colors_list = []
for i in codes:
temp = []
for j in range(1,7,2):
temp.append(int(i[j]+i[j+1],16))
all_colors_list.append(tuple(temp))
all_colors = np.array(all_colors_list)
x = cv2.imread('characterize_github.jpg') #change the file location to your required image
#array_x = np.array(x)
#print(np.shape(array_x))
new_x = cv2.resize(x,(width,height))
color_dict = {}
color_check_arr = []
for i in range(height):
temp_to_cca = []
for j in range(width):
hex_value = "#%02x%02x%02x"%tuple(new_x[i][j])
if hex_value in codes:
temp_to_cca.append(hex_value)
else:
if not tuple(new_x[i][j]) in list(color_dict.keys()):
all_colors_list.append(tuple(new_x[i][j]))
all_colors = np.array(all_colors_list)
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(all_colors)
distances,indices = nbrs.kneighbors(all_colors)
temp_calac_val = all_colors_list[indices[16][1]]
hex_value = "#%02x%02x%02x"%temp_calac_val
color_dict[tuple(new_x[i][j])] = hex_value
all_colors_list = all_colors_list[:-1]
temp_to_cca.append(color_dict[tuple(new_x[i][j])])
color_check_arr.append(temp_to_cca)
for i in range(height):
for j in range(width):
color = fg(abs(codes_dict[color_check_arr[i][j]]))
print(color+character+rst,end="")
print("",end="\n")
print(np.shape(new_x))
cv2.imshow("Github_logo",new_x)
cv2.waitKey(100)
cv2.destroyAllWindows()