-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_resize_PIL_another.py
43 lines (37 loc) · 1.27 KB
/
image_resize_PIL_another.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
import os
from PIL import Image
# 处理单张图片的函数
def chuli_image(file_path, result_path):
# 打开图片
image = Image.open(file_path)
# 获取图片的宽和高
width, height = image.size
# 计算缩放比例
new_width = None
new_height = None
if width < height:
new_width = 720
new_height = 1280
else:
new_width = 1280
new_height = 720
# 缩放图片
image = image.resize((new_width, new_height), Image.ANTIALIAS)
# 保存图片
image.save(result_path)
if __name__=="main":
root_dir = ".\\path to data"
RESULTS_PATH = ".\\path to results"
for subdir, _, files in os.walk(root_dir):
for file in files:
# 判断文件是否为图片格式
if file.endswith(('jpg', 'jpeg', 'png', 'bmp', 'gif')):
# 获取文件的完整路径
file_path = os.path.join(subdir, file)
print("subdir: ", subdir)
item_name = subdir.split('\\')[-1]
result_path = RESULTS_PATH + "\\" + item_name
os.makedirs(result_path, exist_ok=True)
result_path = os.path.join(result_path, file)
# 处理图片
chuli_image(file_path, result_path)