-
Notifications
You must be signed in to change notification settings - Fork 40
/
crop.py
executable file
·46 lines (41 loc) · 1.79 KB
/
crop.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
#! /usr/bin/python
import os
from PIL import Image
def crop_img_by_half_center(src_file_path, dest_file_path):
im = Image.open(src_file_path)
x_size, y_size = im.size
start_point_xy = x_size / 4
end_point_xy = x_size / 4 + x_size / 2
box = (start_point_xy, start_point_xy, end_point_xy, end_point_xy)
new_im = im.crop(box)
new_new_im = new_im.resize((47,55))
new_new_im.save(dest_file_path)
def walk_through_the_folder_for_crop(aligned_db_folder, result_folder):
if not os.path.exists(result_folder):
os.mkdir(result_folder)
i = 0
img_count = 0
for people_folder in os.listdir(aligned_db_folder):
src_people_path = aligned_db_folder + people_folder + '/'
dest_people_path = result_folder + people_folder + '/'
if not os.path.exists(dest_people_path):
os.mkdir(dest_people_path)
for video_folder in os.listdir(src_people_path):
src_video_path = src_people_path + video_folder + '/'
dest_video_path = dest_people_path + video_folder + '/'
if not os.path.exists(dest_video_path):
os.mkdir(dest_video_path)
for img_file in os.listdir(src_video_path):
src_img_path = src_video_path + img_file
dest_img_path = dest_video_path + img_file
crop_img_by_half_center(src_img_path, dest_img_path)
i += 1
img_count += len(os.listdir(src_video_path))
if __name__ == '__main__':
aligned_db_folder = "data/aligned_images_DB"
result_folder = "data/crop_images_DB"
if not aligned_db_folder.endswith('/'):
aligned_db_folder += '/'
if not result_folder.endswith('/'):
result_folder += '/'
walk_through_the_folder_for_crop(aligned_db_folder, result_folder)