-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrim_resize_image.py
116 lines (100 loc) · 3.56 KB
/
trim_resize_image.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
from PIL import Image, ImageChops, ImageOps
import argparse
import os
def scale(image, max_size, method=Image.ANTIALIAS):
im_aspect = float(image.size[0]) / float(image.size[1])
out_aspect = float(max_size[0]) / float(max_size[1])
if im_aspect >= out_aspect:
scaled = image.resize(
(max_size[0], int((float(max_size[0]) / im_aspect) + 0.5)), method)
else:
scaled = image.resize((int((float(max_size[1]) * im_aspect) + 0.5),
max_size[1]), method)
offset = (int((max_size[0] - scaled.size[0]) / 2), int(
(max_size[1] - scaled.size[1]) / 2))
# print(offset)
back = Image.new("RGB", max_size, "white")
back.paste(scaled, offset)
return back
# trim from PIL image
def trim_resize_PIL(image_input, width, height):
bg = Image.new(image_input.mode, image_input.size,
image_input.getpixel((0, 0)))
diff = ImageChops.difference(image_input, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
image_output = image_input.crop(bbox)
image_output = scale(image_output, [width, height])
return image_output
def parse_args():
desc = "ttf/otf fonts to jpg images set (JUST KOREAN)"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
'--img_dir',
type=str,
help='directory that includes images',
required=True)
parser.add_argument(
'--result_dir',
type=str,
default='results',
help='directory where result images is stored to',
required=False)
parser.add_argument(
'--width',
type=int,
default=256,
help='width after resize',
required=False)
parser.add_argument(
'--height',
type=int,
default=256,
help='height after resize',
required=False)
parser.add_argument(
'--border',
type=int,
default=0,
help='white space around result images',
required=False)
return parser.parse_args()
def main():
IMAGE_EXTENTION = ['.jpg', '.jpeg', '.png']
args = parse_args()
img_dir = args.img_dir
result_dir = args.result_dir
if not os.path.exists(img_dir):
print("[Error] Can't find directory [ ./%s ]" % (img_dir))
return
if not os.path.exists(result_dir):
os.makedirs(result_dir)
width = args.width
height = args.height
border = args.border
for path, _, images in os.walk(img_dir):
count = 0
for image in images:
count += 1
ext = os.path.splitext(image)[-1]
if ext not in IMAGE_EXTENTION:
continue
if count % 10 == 0:
print("WORKING:: %s" % (image))
count = 0
with Image.open("%s/%s" % (path, image)) as image_input:
image_output = trim_resize_PIL(image_input, width, height)
image_output = ImageOps.expand(
image_output, border=border, fill='white')
name = os.path.splitext(image)[0]
if '/' in path:
path_out = path.split('/', 1)[-1]
if not os.path.exists(result_dir + '/' + path_out):
os.makedirs(result_dir + '/' + path_out)
else:
path_out = './'
full_name = "%s/%s_%dx%d%s" % (path_out, name, width, height, ext)
image_output.save(os.path.join(result_dir + '/', full_name))
print("!!!done!!!")
if __name__ == '__main__':
main()