-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchange_color.py
More file actions
executable file
·174 lines (140 loc) · 5.27 KB
/
change_color.py
File metadata and controls
executable file
·174 lines (140 loc) · 5.27 KB
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
173
174
#!/usr/bin/env python3
#encoding=utf-8
import os
from os.path import exists
import numpy as np
from PIL import Image
import argparse
def changeColor(im, from_rgb_value, to_rgb_value, mode="MatchAll"):
data = np.array(im)
r1, g1, b1 = from_rgb_value
r2, g2, b2 = to_rgb_value
red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
# default mode=="MatchAll":
mask=None
if mode=="MatchAll":
mask = (red == r1) & (green == g1) & (blue == b1)
if mode=="NotMatch":
mask = (red != r1) & (green != g1) & (blue != b1)
data[:,:,:3][mask] = [r2, g2, b2]
im = Image.fromarray(data)
return im
def main():
parser = argparse.ArgumentParser(description='ChangeColor')
parser.add_argument("--input",
help="input font file",
required=True,
type=str)
parser.add_argument("--output",
help="output font folder",
default=None,
type=str)
parser.add_argument("--from_r",
help="from RED color",
default=0,
type=int)
parser.add_argument("--from_g",
help="from GREEN color",
default=0,
type=int)
parser.add_argument("--from_b",
help="from BLUE color",
default=0,
type=int)
parser.add_argument("--to_r",
help="to RED color",
default=255,
type=int)
parser.add_argument("--to_g",
help="to GREEN color",
default=0,
type=int)
parser.add_argument("--to_b",
help="to BLUE color",
default=0,
type=int)
parser.add_argument("--resize_canvas_size",
help="resize canvas to new size",
default=None,
type=int)
# default is not overwrite, so run twice is okey.
parser.add_argument("--overwrite",
help="force overwrite exist image file",
action='store_true')
# allow fuzzy mode.
parser.add_argument("--fuzziness",
help="select more colors",
default=None,
type=int)
# default is not overwrite, so run twice is okey.
parser.add_argument("--reverse",
help="not match all",
action='store_true')
args = parser.parse_args()
image_file_in = args.input
image_file_out = image_file_in
fuzziness = args.fuzziness
if not args.output is None:
image_file_out = args.output
#print("Open font:", image_file_in)
#print("Save path:", image_file_out)
cnt = 0
if not exists(image_file_in):
print("image file not found:", args.input)
else:
img_raw = Image.open(image_file_in)
# force convert to RGB
img_rgb = Image.new("RGB", img_raw.size, (255, 255, 255))
img_rgb.paste(img_raw)
from_rgb_value = (args.from_r,args.from_g,args.from_b)
to_rgb_value = (args.to_r,args.to_g,args.to_b)
mode="MatchAll"
if args.reverse:
mode="NotMatch"
if not fuzziness:
# only replace 1 color.
img_rgb = changeColor(img_rgb, from_rgb_value, to_rgb_value, mode=mode)
else:
# fuzzy mode.
if fuzziness > 0:
#print("fuzziness: %d" % (fuzziness))
#print("from RGB color: %d,%d,%d" % (args.from_r,args.from_g,args.from_b))
#print("to RGB color: %d,%d,%d" % (args.to_r,args.to_g,args.to_b))
travel_log = []
for direction in range(-1,2,2):
#print("direction: %d" % (direction))
for increase_value in range(fuzziness):
#print("increase_value: %d" % (increase_value))
# PS: not support RGB mode, for GRAY mode now.
target_r = args.from_r + (direction * (increase_value+1))
if target_r <=0:
target_r = 0
if target_r >=255:
target_r = 255
target_g = args.from_g + (direction * (increase_value+1))
if target_g <=0:
target_g = 0
if target_g >=255:
target_g = 255
target_b = args.from_b + (direction * (increase_value+1))
if target_b <=0:
target_b = 0
if target_b >=255:
target_b = 255
#print("target_r: %d" % (target_r))
#print("target_g: %d" % (target_g))
#print("target_b: %d" % (target_b))
from_rgb_value = (target_r, target_g, target_b)
if not from_rgb_value in travel_log:
img_rgb = changeColor(img_rgb, from_rgb_value, to_rgb_value, mode=mode)
travel_log.append(from_rgb_value)
else:
#print("skip this ralace:", from_rgb_value)
pass
if args.resize_canvas_size:
img_rgb = img_rgb.resize( (args.resize_canvas_size, args.resize_canvas_size), Image.ANTIALIAS )
img_rgb.save(image_file_out)
cnt += 1
#print("Convert %d images." % (cnt))
if __name__ == '__main__':
main()