Python 用 OpenCV 显示文字 (6)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011520181/article/details/84110517




利用 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()

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

展开阅读全文

没有更多推荐了,返回首页