import os from PIL import Image def crop_images(root_folder): for root, dirs, files in os.walk(root_folder): for file in files: if file.lower().endswith(('png', 'jpg', 'jpeg', 'bmp', 'gif')): file_path = os.path.join(root, file) with Image.open(file_path) as img: if img.width == 2048 and img.width < img.height: left = 0 top = img.height - 2048 right = 2048 bottom = img.height cropped_img = img.crop((left, top, right, bottom)) cropped_img.save(file_path) print(f'Cropped {file_path}') # 调用函数并传入顶层文件夹路径 root_folder = input("请输入顶层文件夹路径: ") crop_images(root_folder)
标签:img,crop,file,path,folder,root,裁图 From: https://www.cnblogs.com/zly324/p/18245716