cv2.copyMakeBorder()和 cv2.putText()函数测试

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代表什么???没整明白

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jaffe—fly

古人学问无遗力,少壮工夫老始成

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值