导包,获得图片路径
from PIL import Image, ImageDraw
import numpy as np
cameraID_dict = {"31011529001320014340": "(430, 640, 830, 1080)"}
img_path = "D:/dataset/actual_picture/31011529001320014340-2021-04-26-13-05-27.194.jpeg"
cameraID = img_path.split("/")[-1].split("-")[0]
print(cameraID)
PIL读取图片
im = Image.open(img_path)
print("size", im.size)
print(cameraID_dict[cameraID])
PIL 截取部分图片
box = tuple(eval(cameraID_dict[cameraID]))
region = im.crop(box)
# region.show()
PIL 将截取的图粘贴回原图,并保存图片
im.paste(region, box)
draw = ImageDraw.Draw(im)
draw.rectangle(box, fill=None, outline="green", width=3)
im.show()
im.save('./img_merge/001.jpg')