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

查看:24
本文介绍了如何使用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'

查询我不能使用上面的字体类型吗?我在互联网上搜索,但只找到了与 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)
thickness              = 1
lineType               = 2

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

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

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

cv2.waitKey(0)

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

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