-
Notifications
You must be signed in to change notification settings - Fork 0
/
because.py
72 lines (53 loc) · 1.98 KB
/
because.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
import os
import sys
import numpy
from PIL import Image
import bcolors
def main(path):
palette = get_palette_from_image(path)
write_palette_to_file(palette, path)
def write_palette_to_file(palette, path):
file_path = get_file_name(f"{os.path.splitext(path)[0]}.tbp")
print(f"Write palette to \"{file_path}\"")
with open(file_path, 'w') as f:
file = open(file_path, "w")
for i in range(len(palette)): # prefer this, to show what's added rather than writelines()
print(f"{bcolors.OKMSG}Added #{palette[i]} to palette{bcolors.ENDC}")
if i == len(palette) - 1:
file.write(f"{palette[i]}")
else:
file.write(f"{palette[i]}\n")
def get_file_name(path):
filename, extension = os.path.splitext(path)
counter = 1
while os.path.exists(path):
path = f"{filename} ({str(counter)}){extension}"
counter += 1
return path
def get_palette_from_image(path):
image = Image.open(path)
image = image.convert('RGB')
pixels = list(image.getdata())
pixels = numpy.array(pixels).reshape((image.width, image.height, 3))
raw_palette = []
for x in range(image.width):
for y in range(image.height):
raw_palette.append(rgb_to_hex(pixels[x, y]))
return list(set(raw_palette))
def rgb_to_hex(rgb_color):
hex_color = ""
for i in rgb_color:
i = int(i)
hex_color += ("{:02x}".format(i))
return hex_color
if __name__ == '__main__':
print(f"{bcolors.HEADER}===because==={bcolors.ENDC}")
print(f"{bcolors.HEADER}tasBot Eye Color Analyzing Uniquified Search Engine{bcolors.ENDC}")
print(f"{bcolors.HEADER}============={bcolors.ENDC}")
if len(sys.argv) > 1:
image_path = str(sys.argv[1])
print(f"Read image \" {image_path}\"")
main(image_path)
else:
print(f"{bcolors.ERRMSG}Please provide a path to a image file!{bcolors.ENDC}")
print("Usage:\n because <file to image>")