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("裁剪完成")
批量选择裁剪图片
最新推荐文章于 2024-09-14 19:55:48 发布