Python 用 OpenCV 显示文字 (6)

18 篇文章 4 订阅
17 篇文章 3 订阅




利用 OpenCV 自带的 putText() 函数绘制文字 并显示,其函数声明如下:

cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
  • img:要画的圆所在的矩形或图像
  • text:要绘制的文字
  • org:文字在图像中的左下角坐标
  • fontFace:字体,可选 :FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, orFONT_HERSHEY_SCRIPT_COMPLEX, 以上所有类型都可以配合 FONT_HERSHEY_ITALIC使用,产生斜体效果
  • fontScale:字体大小,该值和基础大小相乘得到字体大小
  • color:文字颜色,如 (0, 0, 255) 红色,BGR
  • thickness:字体线条宽度
  • lineType
    • 8 (or omitted) : 8-connected line
    • 4:4-connected line
    • CV_AA - antialiased line
  • bottomLeftOrigin:为 true,图像数据原点在左下角;否则,图像数据原点在左上角

绘制文字的完整代码如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@Time    : 2018-11-13 21:47
@Author  : jianjun.wang
@Email   : alanwang6584@gmail.com

 Font type. One of FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX, 
 FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, 
 or FONT_HERSHEY_SCRIPT_COMPLEX, where each of the font ID’s can be combined with FONT_ITALIC 

"""

import numpy as np
import cv2 as cv
 
img = np.zeros((320, 320, 3), np.uint8) #生成一个空灰度图像
print img.shape # 输出:(320, 320, 3)


text = 'AlanWang4523'
org = (40, 80)
fontFace = cv.FONT_HERSHEY_COMPLEX
fontScale = 1
fontcolor = (0, 255, 0) # BGR
thickness = 1 
lineType = 4
bottomLeftOrigin = 1
# cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType, bottomLeftOrigin)
cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)


text = 'https://blog.csdn.net/u011520181'
org = (10, 180)
fontFace = cv.FONT_HERSHEY_TRIPLEX
fontScale = 0.5
fontcolor = (0, 0, 255) # BGR
thickness = 1 
lineType = 4
bottomLeftOrigin = 1
cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)


cv.namedWindow("image")
cv.imshow('image', img)
cv.waitKey (10000) # 显示 10000 ms 即 10s 后消失
cv.destroyAllWindows()

运行后效果如下:
在这里插入图片描述

Python 安装 OpenCV 及显示图像 (1)

Python 用 OpenCV 画点和圆 (2)

Python 用 OpenCV 画直线 (3)

Python 用 OpenCV 画矩形 (4)

Python 用 OpenCV 画椭圆 (5)

Python 用 OpenCV 显示文字 (6)

  • 9
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值