侧边栏壁纸
博主头像
The Lonely House博主等级

行动起来,活在当下

  • 累计撰写 15 篇文章
  • 累计创建 24 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

Python快速分类横竖屏图片

Solituder
2023-05-16 / 0 评论 / 1 点赞 / 54 阅读 / 897 字 / 正在检测是否收录...
from PIL import Image
import os
import concurrent.futures

input_dir = "D:\\Download\\webpic-part1\\20211201"
output_dir = "D:\\Download"
horizontal_dir = os.path.join(output_dir, "horizontal")
vertical_dir = os.path.join(output_dir, "vertical")
os.makedirs(horizontal_dir, exist_ok=True)
os.makedirs(vertical_dir, exist_ok=True)

def process_image(filename):
    file_path = os.path.join(input_dir, filename)
    with Image.open(file_path) as image:
        width, height = image.size
        if width > height:
            image.save(os.path.join(horizontal_dir, filename))
        else:
            image.save(os.path.join(vertical_dir, filename))
    print("Processed:", filename)

image_filenames = os.listdir(input_dir)
with concurrent.futures.ThreadPoolExecutor(max_workers=12) as executor:
    executor.map(process_image, image_filenames)
1

评论区