批量选择裁剪图片

from PIL import Image
import os

# 定义输入图像文件夹和输出图像文件夹
input_folder = "F:\\对比图\\yolox\\yolox_result2"
output_folder = "F:\\对比图\\yolox\\crop"

# 创建输出文件夹
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 定义裁剪的位置和尺寸
# 这里的坐标是左上角点的坐标 (left, top),和右下角点的坐标 (right, bottom)
crop_coordinates = [(5472, 0, 10944, 3648)]  # 例如:(left, top, right, bottom)
#                    (50, 60, 250, 350)]  # 添加更多裁剪区域

# 遍历输入文件夹中的所有图像文件
for filename in os.listdir(input_folder):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        input_path = os.path.join(input_folder, filename)
        output_path = os.path.join(output_folder, filename)

        # 打开图像
        image = Image.open(input_path)

        # 根据定义的坐标裁剪图像
        for i, (left, top, right, bottom) in enumerate(crop_coordinates):
            cropped_image = image.crop((left, top, right, bottom))
            cropped_image.save(os.path.join(output_folder, f"{i}_{filename}"))

        # 关闭原始图像
        image.close()

print("裁剪完成")

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值