筛图(删除窄图)

import os
from PIL import Image
import tqdm

def delete_images_with_height_greater_than_width(folder_path):
    # 遍历文件夹
    for root, dirs, files in os.walk(folder_path):
        for file in tqdm.tqdm(files, desc="处理中"):
            if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
                try:
                    file_path = os.path.join(root, file)
                    with Image.open(file_path) as img:
                        width, height = img.size

                    # 如果宽度小于高度,则删除图片
                    if width < height:
                        os.remove(file_path)
                        print(f"删除图片: {file_path}")
                except Exception as e:
                    print(f"无法处理图片 {file}: {e}")

if __name__ == "__main__":
    folder_path = input("请输入母文件夹的路径: ")
    delete_images_with_height_greater_than_width(folder_path)

 

posted @ 2024-01-25 18:36  不上火星不改名  阅读(36)  评论(0)    收藏  举报