-
Notifications
You must be signed in to change notification settings - Fork 0
/
skribblify.py
172 lines (135 loc) · 6.35 KB
/
skribblify.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# ==================
# Skribblify
# ------------------
# Author: Remarci225
# ==================
# Imports
# =======
from argparse import ArgumentParser
from pathlib import Path
from os import listdir
from PIL import Image
from math import cbrt
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import cpu_count
# Constants
# =========
SUPPORTED_FORMATS = ["png", "jpg", "jpeg"]
COLORS = [
((255, 255, 255), (100.0, 0.0019288242798509714, -0.009795878399820879)),
((0, 0, 0), (0.0, 0.0, 0.0)),
((193, 193, 193), (78.06804360790544, 0.0015641441937774836, -0.00794378548703012)),
((80, 80, 80), (34.02862316443873, 0.0008318657159223086, -0.004224778526118467)),
((239, 19, 11), (50.50022617975232, 74.75290834352649, 61.34942916415176)),
((116, 11, 7), (23.56433203460626, 42.58351869447127, 32.34777184120819)),
((255, 113, 0), (64.21775468969531, 50.02541962305684, 72.38859747427199)),
((194, 56, 0), (44.656850102788184, 53.08621652978424, 56.50251877487948)),
((255, 228, 0), (90.20494969194934, -8.446064549690435, 89.46493336189324)),
((232, 162, 0), (71.5525721051447, 15.744399015153675, 75.42411080881635)),
((0, 204, 0), (71.68084944389129, -72.84720577427306, 70.30681927414962)),
((0, 70, 25), (25.108133027946373, -31.20696976521603, 21.343399970244555)),
((0, 255, 145), (88.71617210498218, -73.97128676678655, 38.71231872367462)),
((0, 120, 93), (44.55141873539338, -35.73003893347959, 6.826555536129675)),
((0, 178, 255), (68.79527662821968, -10.666196452199127, -48.43772599769729)),
((0, 86, 158), (36.22232960379765, 6.938045026812056, -45.12430141662922)),
((35, 31, 211), (29.514279243755468, 61.04099296881776, -87.39672424350273)),
((14, 8, 101), (10.60606857513346, 36.29770749025354, -51.088293947049614)),
((163, 0, 186), (40.133616231397845, 73.92803762808725, -54.77237926445477)),
((85, 0, 105), (19.847453396211172, 47.79255106167757, -38.17420510403695)),
((223, 105, 167), (60.41017669793527, 52.86073142013259, -11.304080438825403)),
((135, 53, 84), (34.67549431166724, 38.28994886793446, -0.7658436719981565)),
((255, 172, 142), (77.708232377821, 27.075329441481333, 27.82550824892558)),
((204, 119, 77), (58.5712296550886, 29.41638474180103, 37.094912585289144)),
((160, 82, 45), (43.796139581025685, 29.324941748803845, 35.63667996216117)),
((99, 48, 13), (26.14434788696905, 20.426380558580703, 31.121640342598912))]
LAB_VALUES = {}
ONE_THIRD = 1 / 3
DELTA = 6 / 29
INVERSE_DELTA = 29 / 6
CUBED_DELTA = DELTA ** 3
FOUR_TWENTYNINTH = 4 / 29
XN = 95.0489
YN = 100
ZN = 108.8840
# Functions
# =========
def get_full_path(path):
relative_markers = ['.', '/', '\\']
stripped_path = path
for relative_marker in relative_markers:
stripped_path = stripped_path.lstrip(relative_marker)
current_path = Path().cwd()
new_path = current_path if path == "" else Path(current_path, stripped_path) if path[0] in relative_markers else path
return new_path if Path(new_path).exists() else current_path
def f(t):
return cbrt(t) if t > CUBED_DELTA else ONE_THIRD * (INVERSE_DELTA ** 2) * t + FOUR_TWENTYNINTH
def rgb_to_lab(rgb):
R = rgb[0] / 255
G = rgb[1] / 255
B = rgb[2] / 255
R2 = (((R + 0.055) / 1.055) ** 2.4 if R > 0.04045 else R / 12.92) * 100
G2 = (((G + 0.055) / 1.055) ** 2.4 if G > 0.04045 else G / 12.92) * 100
B2 = (((B + 0.055) / 1.055) ** 2.4 if B > 0.04045 else B / 12.92) * 100
X = 0.4124 * R2 + 0.3576 * G2 + 0.1805 * B2
Y = 0.2126 * R2 + 0.7152 * G2 + 0.0722 * B2
Z = 0.0193 * R2 + 0.1192 * G2 + 0.9505 * B2
L = 116 * f(Y / YN) - 16
a = 500 * (f(X / XN) - f(Y / YN))
b = 200 * (f(Y / YN) - f(Z / ZN))
return (L, a, b)
def get_difference(values, new_values):
deltaL = values[0] - new_values[0]
deltaA = values[1] - new_values[1]
deltaB = values[2] - new_values[2]
return deltaL * deltaL + deltaA * deltaA + deltaB * deltaB
def change_pixel(start_index, batch_count):
for i in range(start_index, start_index + batch_count):
pixel = data[i]
if pixel not in LAB_VALUES:
values = rgb_to_lab(pixel)
LAB_VALUES[pixel] = values
else:
values = LAB_VALUES[pixel]
min_index = 0
min_difference = get_difference(values, COLORS[min_index][1])
for j in range(1, len(COLORS)):
difference = get_difference(values, COLORS[j][1])
if difference < min_difference:
min_index = j
min_difference = difference
data[i] = COLORS[min_index][0]
# Operations
# ==========
parser = ArgumentParser(
prog="Skribblify",
description="Convert images to the skribbl.io color palette"
)
parser.add_argument("-i", type=str, action="store", dest="input", default="", required=False,
help="input path for images, current directory by default")
parser.add_argument("-o", type=str, action="store", dest="output", default="", required=False,
help="output path for images, current directory by default")
parser.add_argument("-p", type=str, action="store", dest="prefix", default="converted ", required=False,
help="prefix for the generated image filenames, \"converted \" by default")
arguments = parser.parse_args()
input_path = get_full_path(arguments.input)
output_path = get_full_path(arguments.output)
prefix = arguments.prefix
files = listdir(input_path)
image_files = [file for file in files if file.split('.')[-1] in SUPPORTED_FORMATS and file[:9] != "converted"]
print("Starting skribblifying...")
for image_file in image_files:
print(f"Skribblifying {image_file}...")
image = Image.open(image_file)
data = list(image.getdata())
pixel_count = len(data)
thread_count = cpu_count() * 2
threadPoolExecutor = ThreadPoolExecutor(thread_count)
batch_count = int(pixel_count / thread_count)
last_batch_count = batch_count + pixel_count - thread_count * batch_count
for i in range(thread_count - 1):
threadPoolExecutor.submit(change_pixel, i * batch_count, batch_count)
threadPoolExecutor.submit(change_pixel, pixel_count - last_batch_count, last_batch_count)
threadPoolExecutor.shutdown()
image.putdata(data)
image.save(f"{Path(output_path, f'{prefix}{image_file}')}")
print("Skribblifying Done! :D")