1.函数封装
'''
函数描述:对给定的图片和坐标信息在图片上标框,并在框的上方标注出该框的名称
函数参数:img_file_path=图片的绝对路径,new_img_file_path=保存后的绝对路径,points=[(str,[b0,b1,b2,b3])]
返回值:无返回值
注意事项:坐标[b0,b1,b2,b3]依次为左上角和右下角的坐标
'''
def draw_rectangle_by_point(img_file_path,new_img_file_path,points):
image = cv2.imread(img_file_path)
for item in points:
print("当前字符:",item)
point=item[1]
first_point=(int(point[0])*2,int(point[1])*2)
last_point=(int(point[2])*2,int(point[3])*2)
print("左上角:",first_point)
print("右下角:",last_point)
cv2.rectangle(image, first_point, last_point, (0, 255, 0), 1)
cv2.putText(image, item[0], first_point, cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5, color=(255,0,0), thickness=1)
cv2.imwrite(new_img_file_path, image)
2.测试样例
if __name__ == '__main__':
points=[('No. 18', [89.72196203867594, 193.4539794921875, 117.3230328241984, 207.30389404296875]),
('Zhongshan Dist.', [241.5278513590495, 193.4539794921875, 310.5305283228556, 207.30389404296875])]
draw_rectangle_by_point(r'E:\wm_workspace\day2_2_24\images\16.pdf.png',"new.jpg",points)
3.结果展示