Problem Description:
There is a folder with several pictures and directories in it, and several directories and pictures in its subdirectories... Now it is necessary to adjust the size of all the pictures in the root directory (including the pictures in its subdirectories).
The contents of the root folder are as follows:
The contents of the subdirectories are as follows:
The current picture size is: 400*700
Code:
import os
from PIL import Image
# 图片所在的路径(根目录)
img_path = r"D:\素材"
# 获取该文件夹下所有的图片
for root, dirs, file_list in os.walk(img_path):
for file_name in file_list:
# 获取所有图片文件的路径
all_file_path = os.path.join(root, file_name)
file = Image.open(all_file_path)
# 以高质量修改图片尺寸为(500,700)
out = file.resize((500, 700), Image.ANTIALIAS)
# 以同名保存到原路径
out.save(all_file_path)