opencv学习笔记(五):图片添加文字
在图片上添加文字 cv2.putText()
函数原型:cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]] )
参数说明:img Image.
text Text string to be drawn.
org Bottom-left corner of the text string in the image.
fontFace Font type, see HersheyFonts.
fontScale Font scale factor that is multiplied by the font-specific base size.
color Text color.
thickness Thickness of the lines used to draw a text.
lineType Line type. See LineTypes
bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
img: 要显示的图片
text: 要显示的文字
org: 图像中文本字符串左下角坐标值
fontFace: 字体类型
fontScale:字体缩放系数
color 字体颜色
thickness 线宽
lineType 线型
bottomLeftOrigin 如果取值为TRUE,则Point org指定的点为插入文字的左上角位置,如果取值为默认值false则指定点为插入文字的左下角位置,取值影响文字位置基点。
关于fontFace参数的取值,如下表所示:
FONT_HERSHEY_SIMPLEX:正常大小无衬线字体
FONT_HERSHEY_PLAIN:小号无衬线字体
FONT_HERSHEY_DUPLEX:正常大小无衬线字体,比FONT_HERSHEY_SIMPLEX更复杂
FONT_HERSHEY_COMPLEX:正常大小有衬线字体
FONT_HERSHEY_TRIPLEX:正常大小有衬线字体,比FONT_HERSHEY_COMPLEX更复杂
FONT_HERSHEY_COMPLEX_SMALL:FONT_HERSHEY_COMPLEX的小译本
FONT_HERSHEY_SCRIPT_SIMPLEX:手写风格字体
FONT_HERSHEY_SCRIPT_COMPLEX:手写风格字体,比FONT_HERSHEY_SCRIPT_SIMPLEX更复杂
这些参数和FONT_ITALIC同时使用就会得到相应的斜体字
添加文字实验:
import cv2
import numpy as np
img = np.zeros((400,400,3),np.uint8)
cv2.putText(img,'Restar_xt',(100,200),cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,1,(255,255,255),2,cv2.LINE_AA)
cv2.imshow('img',img)
cv2.waitKey(0)
运行结果如下: