python opencv 如何给图片添加文字?cv2.putText() PIL
生活随笔
收集整理的这篇文章主要介绍了
python opencv 如何给图片添加文字?cv2.putText() PIL
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
参考文章1:python如何在图片上添加文字(中文和英文)Python在图片上添加文字的两种方法:OpenCV和PIL
参考文章2:python之------如何在图片上面添加文字(多种类型的文字)【PIL】
参考文章3:python 图片上添加中文文字【PIL】
参考文章4:python-OpenCV之在图片上添加文字(cv.putText())
文章目录
- putText函数doc
- 示例代码
- 功能
- code
- 最终效果
putText函数doc
opencv版本:4.1.0(不同版本Doc也许不同)
def putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None): # real signature unknown; restored from __doc__"""putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img. @brief Draws a text string. 绘制文本字符串。. . The function cv::putText renders the specified text string in the image. Symbols that cannot be rendered. using the specified font are replaced by question marks. See #getTextSize for a text rendering code. example.· 函数cv :: putText在图像中呈现指定的文本字符串。 无法使用指定字体呈现的符号将替换为问号。 有关文本渲染代码示例,请参见#getTextSize。. . @param img Image. 图片。. @param text Text string to be drawn. 要绘制的文本字符串。. @param org Bottom-left corner of the text string in the image. 图像中文本字符串的左下角。. @param fontFace Font type, see #HersheyFonts. 字体类型,请参见#HersheyFonts。. @param fontScale Font scale factor that is multiplied by the font-specific base size.字体比例因子乘以特定于字体的基本大小。. @param color Text color. 文字颜色。. @param thickness Thickness of the lines used to draw a text. 用于绘制文本的线条的粗细。. @param lineType Line type. See #LineTypes 线型。 请参阅#LineTypes. @param bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.如果为true,则图像数据原点位于左下角。 否则,它位于左上角。"""passDoc中所指#HersheyFonts:链接
Doc中所指#LineType:链接
示例代码
功能
将上上级imgs文件夹内的2000张图片全部打上标记,显示其模糊程度值(越小越模糊)(用于展示用)
code
# -*- encoding: utf-8 -*- """ @File : judge_the_picture_blur.py @Time : 2019/10/25 8:52 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import cv2 import os# 返回指定路径图像的拉普拉斯算子边缘模糊程度值 def getImageVar(img_path):image = cv2.imread(img_path)img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)imageVar = cv2.Laplacian(img2gray, cv2.CV_64F).var()return imageVar# 返回给定文件夹下所有图片的路径列表 def listFolderImgPath(folder_img_path):img_path_list = []for filename in os.listdir(folder_img_path):filepath = os.path.join(folder_img_path, filename)img_path_list.append(filepath)return img_path_list# 给单张图片添加文字(图片路径,文字) def writeText(img_path, text):# 加载背景图片# img的类型是np.ndarray数组img = cv2.imread(img_path)# 在图片上添加文字信息# 颜色参数值可用颜色拾取器获取((255,255,255)为纯白色)# 最后一个参数bottomLeftOrigin如果设置为True,那么添加的文字是上下颠倒的composite_img = cv2.putText(img, text, (100, 680), cv2.FONT_HERSHEY_SIMPLEX,2.0, (255, 255, 255), 5, cv2.LINE_AA, False)cv2.imwrite(img_path, composite_img)# 文件夹路径 folder_img_path = '../../imgs/'# 图片路径 img_path = '../../imgs/f_cotton-g_top (813).jpg'# print(getImageVar(img_path))# print(listFolderImgPath(folder_img_path))# 获取图片路径列表 img_path_list = listFolderImgPath(folder_img_path)# 循环处理每张图片 for img_path in img_path_list:# 获取该张图片模糊值imageVar = getImageVar(img_path)# 创建需写入文字信息text = 'The fuzzy is: {:.2f}'.format(imageVar)# 将文字写入图片writeText(img_path, text)# img = cv2.imread(img_path)# cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)# cv2.imshow('image', img)# cv2.waitKey(1)最终效果
总结
以上是生活随笔为你收集整理的python opencv 如何给图片添加文字?cv2.putText() PIL的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: python 文件操作 os.path
- 下一篇: python 如何将代码打包成exe可执