图像处理库Pillow的使用:批量裁剪图片

from PIL import Image
import numpy as np
import os
import hashlib

input_folder = "D:/tmp/luping/shots/"
output_folder = "D:/tmp/luping/shots_clean/"
os.makedirs(output_folder, exist_ok=True)

bottom_crop = 600  # 要裁掉的底部高度

for file in sorted(os.listdir(input_folder)):
    if not file.lower().endswith((".png", ".jpg", ".jpeg")):
        continue

    path = os.path.join(input_folder, file)
    img = Image.open(path).convert("L")  # 灰度
    w, h = img.size

    # 裁剪底部空白
    bottom = h - bottom_crop
    crop = img.crop((0, 60, w, bottom))  # 保留顶部到 bottom (左, 上, 右, 下)
       

    # 保存
    crop.save(os.path.join(output_folder, file))

print("完成!已裁剪底部空白")

 

posted @ 2025-12-06 08:52  daviyoung  阅读(0)  评论(0)    收藏  举报