
python opencv 如何给图片添加文字?cv2.putText() PIL
Doc中所指#LineType:
发布日期: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:




示例代码
功能
将上上级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)
最终效果

发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月01日 07时04分43秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
如何使用Linux命令查看端口是否被占用
2021-05-11
Redis——服务器
2021-05-11
iOS KVC
2021-05-11
返回值(null)和<null>处理【空指针和野指针】
2021-05-11
iOS 宏定义的使用与规范
2021-05-11
CoreText(四):行 CTLineRef
2021-05-11
CoreText(五):省略号
2021-05-11
iOS 8:一、tableView右滑显示选择
2021-05-11
iOS 11:一、导航栏变化
2021-05-11
解决hadoop出现Warning: fs.defaultFS is not set异常
2021-05-11
Android开发之获取常用android设备参数信息
2021-05-11
Android开发之NDK下载与NDK更新
2021-05-11
Android开发之混淆文件配置的混淆规则
2021-05-11
Jenkins打包之本地远程自动打包教程
2021-05-11
Android开发之ADB常用命令
2021-05-11
【SQLI-Lab】靶场搭建
2021-05-11
阿里云盘每天免费领取1.5T容量!附福利码。
2021-05-11
私链下智能合约的简单部署
2021-05-11
java——如何停止一个线程
2021-05-11
Java常见面试题Hashmap的结构,jdk1.7和jdk1.8有哪些区别
2021-05-11