📅  最后修改于: 2020-08-30 03:06:16             🧑  作者: Mango
在Opencv的实际应用中,我们经常可以为图像增加一些log信息提高演示、展示的效果;比如在图像中添加时间、地点等信息。
因此,在本节中我们学习opencv中的putText的用法。
我们先提供一段简单的代码:
import numpy as np
import cv2
image = cv2.imread('sample.png',cv2.IMREAD_UNCHANGED)
position = (10,50)
cv2.putText(
image, #numpy array on which text is written
"Python Examples", #text
position, #position at which writing has to start
cv2.FONT_HERSHEY_SIMPLEX, #font family
1, #font size
(209, 80, 0, 255), #font color
3) #font stroke
cv2.imwrite('output.png', image)
从cv2.putText()的调用方法可以看出,可设置的参数有:
FONT_HERSHEY_PLAIN
FONT_HERSHEY_DUPLEX
FONT_HERSHEY_COMPLEX
FONT_HERSHEY_TRIPLEX
FONT_HERSHEY_COMPLEX_SMALL
FONT_HERSHEY_SCRIPT_SIMPLEX
FONT_HERSHEY_SCRIPT_COMPLEX
FONT_ITALIC
最后展示实际的效果:
知道在图像上书写的文字的形状(宽度,高度),则可以将其放置在图像上居中的位置。
import numpy as np
import cv2
image = cv2.imread('sample.png',cv2.IMREAD_UNCHANGED)
position = ((int) (image.shape[1]/2 - 268/2), (int) (image.shape[0]/2 - 36/2))
cv2.putText(
image, #numpy array on which text is written
"Python Examples", #text
position, #position at which writing has to start
cv2.FONT_HERSHEY_SIMPLEX, #font family
1, #font size
(209, 80, 0, 255), #font color
3) #font stroke
cv2.imwrite('output.png', image)
演示效果如下: