bottom_border = 60
IMG_PATH = 'D:\\study\\GitHub\\PycharmProjects\\mAP\\mAP\\input\\images-optional'
BLACK = [0, 0, 0]
img = cv2.imread(IMG_PATH + "/" + '2007_000033.jpg')
img = cv2.copyMakeBorder(img, 0, bottom_border, 0, 0, cv2.BORDER_CONSTANT,value=BLACK)#以固定填充方式。填充黑色
# 0, bottom_border, 0, 0代表只在图像的下面填充
cv2.imshow("img_copyMakeBorder",img)
cv2.waitKey(0)
img的原图为
填充后
二 cv2.putText()
def draw_text_in_image(img, text, pos, color, line_width):
font = cv2.FONT_HERSHEY_PLAIN
fontScale = 1
lineType = 1
bottomLeftCornerOfText = pos
cv2.putText(img, text,
bottomLeftCornerOfText,
font,
fontScale,
color,
lineType)
text_width, _ = cv2.getTextSize(text, font, fontScale, lineType)[0] # 获取绘制字体的宽和高
#print(cv2.getTextSize(text, font, fontScale, lineType))#((209, 10), 6)
return img, (line_width + text_width)
margin = 10
bottom_border = 60
IMG_PATH = 'D:\\study\\GitHub\\PycharmProjects\\mAP\\mAP\\input\\images-optional'
BLACK = [0, 0, 0]
white = (255, 255, 255)
img = cv2.imread(IMG_PATH + "/" + '2007_000033.jpg')
print(img.shape)#(480, 640, 3)
img = cv2.copyMakeBorder(img, 0, bottom_border, 0, 0, cv2.BORDER_CONSTANT,value=BLACK)
height, widht = img.shape[:2]#(540, 640, 3)
v_pos = int(height - margin - (bottom_border / 2.0))
text = "Image: " + '2007_000033.jpg' + " "
img, line_width = draw_text_in_image(img, text, (margin, v_pos), white, 0)
返回的图像
cv2.putText()和 cv2.getTextSize()经常一起使用
cv2.getTextSize()的返回值((209, 10), 6),前面的(209, 10)代表text的宽和高,后面的6代表什么???没整明白