Python + 批量裁剪图片

from PIL import Image
import os

srcPath = 'D:\\PythonProject\\yolov5-master\\data\\images\\test\\data802\\'   # 所要读取修改的文件夹
dstPath = 'D:\\PythonProject\\yolov5-master\\data\\images\\test\\data1\\'    # 修改后所存放路径
filelist = os.listdir(srcPath)

list1=[]
# 读取图片
for filename in filelist:
    filename1 = os.path.splitext(filename)[1]  # 读取文件后缀名
    filename0 = os.path.splitext(filename)[0]  # 读取文件名
    list1.append(filename0+filename1)

for i in range(0,len(list1)):
    filea = str(srcPath+list1[i])
    img_1 = Image.open(filea)
    # 设置裁剪的位置
    crop_box = (210,272,1622,1044)
    # 裁剪图片
    img_2 = img_1.crop(crop_box)
    #保存图片
    img_2.save(dstPath+list1[i])
print('已经截图成功')

你可能感兴趣的:(机器学习,python)