如何使用python opencv2在Windows中的图像上写文本 [英] How to write text on a image in windows using python opencv2

查看:107
本文介绍了如何使用python opencv2在Windows中的图像上写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在图像上放一些文字. 我将代码编写为:

I want to put some text on an Image. I am writing the code as:

cv2.putText(image,"Hello World!!!", (x,y), cv2.CV_FONT_HERSHEY_SIMPLEX, 2, 255)

出现错误,表示模块"对象没有属性"CV_FONT_HERSHEY_SIMPLEX"

It gives ERROR, saying 'module' object has no attribute 'CV_FONT_HERSHEY_SIMPLEX'

查询 我不能使用上述字体类型吗?我在Internet上进行了搜索,但仅发现与initFont的Opencv C ++有关的语法. 然后我想到了使用putText传递字体类型作为参数. 但这对我不起作用.

Query Can't I use the font type as above? I searched in internet, but found only the syntax related to to Opencv C++ for initFont. Then I thought of using putText to pass the font type as parameter. But it is not working for me.

有什么建议吗?

推荐答案

此代码使用cv2.putText在图像上覆盖文本.您需要安装NumPy和OpenCV.

This code uses cv2.putText to overlay text on an image. You need NumPy and OpenCV installed.

import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Write some Text

font                   = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (10,500)
fontScale              = 1
fontColor              = (255,255,255)
lineType               = 2

cv2.putText(img,'Hello World!', 
    bottomLeftCornerOfText, 
    font, 
    fontScale,
    fontColor,
    lineType)

#Display the image
cv2.imshow("img",img)

#Save image
cv2.imwrite("out.jpg", img)

cv2.waitKey(0)

这篇关于如何使用python opencv2在Windows中的图像上写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆