-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize_all_image.py
123 lines (102 loc) · 3.63 KB
/
resize_all_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
117
118
119
120
121
122
123
from PIL import Image, ImageOps
import os
import glob
import math
from tqdm import tqdm
from resizeimage import resizeimage
from skimage import color
from skimage import io
#
file_location = './txt_white_bn/'
file_location_black = './txt_black_bn/'
image_save_path_txt_white = "./resized_doc_bn_596/"
image_save_path_txt_black = "./resized_mask_bn_596/"
image_file_directory = os.path.join(file_location, '*.png')
image_file_list = glob.glob(image_file_directory)
# image_file_list = image_file_list[1300:1310:1]
def get_max_img_height_from_direcory(direcory):
image_file_directory = os.path.join(direcory, '*.png')
image_file_list = glob.glob(image_file_directory)
index = 0
max_height = 0
for image in image_file_list:
image = Image.open(image)
width, height = image.size
if max_height < height:
max_height = height
index += 1
return max_height
# print(get_max_img_height_from_direcory(file_location))
def resize_image_from_direcory(source_dir, target_dir):
image_file_directory = os.path.join(source_dir, '*.png')
image_file_list = glob.glob(image_file_directory)
index = 0
max_height = get_max_img_height_from_direcory(source_dir)
# max_height = 768
# rounding up to *10
# max_height = int(math.ceil(max_height / 10.0)) * 10
pbar = tqdm(total=len(image_file_list))
for image in image_file_list:
image = Image.open(image).convert('L')
image = image.crop((0, 0, 1000, max_height))
image = ImageOps.fit(image, (max_height, max_height), Image.ANTIALIAS)
image.thumbnail((596,596), Image.ANTIALIAS)
#
# image = resizeimage.resize_contain(image, [max_height, max_height])
# width, height = image.size
# print("resized image {id}. new size is: ".format(id=index), image.size)
image.save(target_dir + "{id}.png".format(id=index))
index += 1
pbar.update(1)
return True
if resize_image_from_direcory(file_location, image_save_path_txt_white) is True:
print("All white resize job done. :) ")
if resize_image_from_direcory(file_location_black, image_save_path_txt_black) is True:
print("All black resize job done. :) ")
######invert images##############
# from PIL import Image
# import PIL.ImageOps
# import os
# import glob
#
# file_location_black = './raw_doc_resized/'
# image_save_path_txt_black = "./x/"
#
# image_file_directory = os.path.join(file_location_black, '*.png')
# image_file_list = glob.glob(image_file_directory)
#
# index = 0
# for image in image_file_list:
# image = Image.open(image).convert('L')
# image = PIL.ImageOps.invert(image)
# width, height = image.size
# print("resized image {id}. new size is: ".format(id=index), image.size)
# image.save(image_save_path_txt_black + "{id}.png".format(id=index))
# index += 1
##### Transparent to black bg#############
# import os
# import glob
#
# file_location_black = './raw_mask_resized/'
# image_save_path_txt_black = "./raw_mask_resized/"
#
# image_file_directory = os.path.join(file_location_black, '*.png')
# image_file_list = glob.glob(image_file_directory)
#
#
#
# from PIL import Image
# index = 0
# for image in image_file_list:
# image = Image.open(image)
# pixdata = image.load()
# width, height = image.size
# for y in xrange(height):
# for x in xrange(width):
# if pixdata[x, y] == (255, 255, 255, 0):
# pixdata[x, y] = (0, 0, 0, 255)
#
# width, height = image.size
# print("resized image {id}. new size is: ".format(id=index), image.size)
# image.save(image_save_path_txt_black + "{id}.png".format(id=index))
# index += 1