PIL读取图片,截取图片并返回原图

导包,获得图片路径

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)
原图.png

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()
截取图.png

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')
返回原图.png

你可能感兴趣的:(PIL读取图片,截取图片并返回原图)