ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

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

图像处理库Pillow的使用:批量裁剪图片
from PIL import Image
import numpy as np
import os
import hashlibinput_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")):continuepath = os.path.join(input_folder, file)img = Image.open(path).convert("L")  # 灰度w, h = img.size# 裁剪底部空白bottom = h - bottom_cropcrop = img.crop((0, 60, w, bottom))  # 保留顶部到 bottom (左, 上, 右, 下)# 保存
    crop.save(os.path.join(output_folder, file))print("完成!已裁剪底部空白")

 

返回列表