-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_image.py
66 lines (50 loc) · 2.26 KB
/
do_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
from PIL import Image, ImageOps
import os
def find_all_image(dir_name):
# Do list with all file in directory
list_files = os.listdir(rf'{dir_name}')
# Images list
list_img_text = []
# Delete all files except image files
for file in list_files:
file_type = file.lower().split('.')[-1]
if file_type == 'png' or file_type == 'jpg' or file_type == 'jpeg':
# Add image to images list
list_img_text.append(file)
return list_img_text
def resize_images(list_img_text, dir_path, new_dir_name, max_px: int):
max_length_vertical = 0
max_length_horizontal = 0
for img_text in list_img_text:
# Load image with orientation
image = ImageOps.exif_transpose(Image.open(rf'{dir_path}/{img_text}'))
width_img, height_img = image.size
# Do new directory
if not os.path.isdir(rf'.\images\{new_dir_name}'):
os.mkdir(rf'.\images\{new_dir_name}')
# Check position image
if width_img > height_img:
scale = width_img / max_px
width_img_resize = max_px
height_img_resize = int(height_img / scale)
if height_img_resize > max_length_vertical:
max_length_vertical = height_img_resize
if width_img_resize > max_length_horizontal:
max_length_horizontal = width_img_resize
img_resize = image.resize((width_img_resize, height_img_resize))
img_resize.save(rf'.\images\{new_dir_name}\{img_text.split(".")[0]}.png')
else:
scale = height_img / max_px
height_img_resize = max_px
width_img_resize = int(width_img / scale)
if height_img_resize > max_length_vertical:
max_length_vertical = height_img_resize
if width_img_resize > max_length_horizontal:
max_length_horizontal = width_img_resize
img_resize = image.resize((width_img_resize, height_img_resize))
img_resize.save(rf'.\images\{new_dir_name}\{img_text.split(".")[0]}.png')
# Resize default image
img_default = Image.open(rf'.\images\0.png').resize((max_length_horizontal, max_length_vertical))
img_default.save(rf'.\images\{new_dir_name}\0.png')
if __name__ == "__main__":
pass