使用cv2.putText()可在图像上添加文本,参数包括图像、文本内容、位置、字体、大小、颜色、粗细和线型,支持多种字体类型,但仅限ASCII字符,中文需借助PIL实现。

在Python中使用OpenCV为图像添加文本,主要通过 cv2.putText() 函数实现。这个函数可以将指定的字符串绘制到图像上,常用于图像标注、结果显示等场景。
cv2.putText(img, text, org, font, fontScale, color, thickness, lineType)
参数说明:
import cv2
<h1>读取图像</h1><p>image = cv2.imread('example.jpg')</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/2064">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680148052964.png" alt="图像转图像AI">
</a>
<div class="aritcle_card_info">
<a href="/ai/2064">图像转图像AI</a>
<p>利用AI轻松变形、风格化和重绘任何图像</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="图像转图像AI">
<span>65</span>
</div>
</div>
<a href="/ai/2064" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="图像转图像AI">
</a>
</div>
<p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><h1>添加文本</h1><p>cv2.putText(image,
text='Hello OpenCV',
org=(50, 100),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1.2,
color=(0, 255, 0), # 绿色
thickness=2,
lineType=cv2.LINE_AA)</p><h1>显示结果</h1><p>cv2.imshow('Image with Text', image)
cv2.waitKey(0)
cv2.destroyAllWindows()</p>OpenCV 提供了多种内置字体,可根据需要选择:
基本上就这些。掌握 putText 函数后,可以在图像中标注关键信息,比如人脸检测中的标签、识别结果等,非常实用。
以上就是python中用OpenCV在图像添加文本的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号