python opencv 如何给图片添加文字?cv2.putText() PIL
发布日期:2021-05-10 08:59:55 浏览次数:25 分类:精选文章

本文共 3154 字,大约阅读时间需要 10 分钟。

文章目录

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,则图像数据原点位于左下角。 否则,它位于左上角。    """    pass

Doc中所指#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 cv2import 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)

最终效果

在这里插入图片描述

在这里插入图片描述

上一篇:yolo-v2 自己的数据集训练以及测试流程(仅供内部使用!)
下一篇:python 文件操作 os.path.join(path, *paths) 路径合成(追加)

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年04月01日 07时04分43秒