Python OpenCV 自动生成快乐源泉小瓶子图片(OpenCV中文写字)
目的
空间看到很多小瓶子,大部分是由P图产生的,会影响图片清晰和质量,而且速度较慢。所以打算使用OpenCV自动生成
步骤
瓶子
没有找到单独的瓶子原图,所以截取了一个,有很多的噪点和不清晰,所以OpenCV进行滤波
import cv2 as cv
import numpy as np
from PIL import ImageFont, ImageDraw, Image# 导入瓶子并滤波
bottle = cv.imread("bottle.jpg")
bottle2 = cv.fastNlMeansDenoisingColored(bottle)
图像参数
设计可调节输入几行几列,并可拟定题目和每一个瓶子的名称
每个瓶子左右相隔0.4倍瓶宽,上下相隔0.8瓶高,整体图片上下各留1倍瓶高
# 输入参数
row = int(input("行:"))
col = int(input("列:"))
title = input("题目:")
print("输入文字: ")
writ = []
for i in range(row*col):zi = input("{}:".format(i+1))writ.append(zi)# 瓶子左右间距、上下间距
span_w = 0.4
span_h = 0.8
画瓶子
图片叠加:
对于在一张图片上粘贴叠加另一张图片,可以采用像素处理
构建背景:
纯色画布背景的构建,在c++中有Mat函数,在Python中没有找到类似功能。所以使用 numpy.ones,构建单位矩阵,255,255,255为白色,所以乘255。若黑色背景,则 np.zeros
遍历每一行每一列,对于每个瓶子的位置,遍历瓶子图片,改变像素点
注意:坐标中,第一项为y,第二项为x
(bottle_h, bottle_w, _) = bottle2.shapebackground2 = np.ones((int((row+row*span_h-span_h+2)*bottle_h), int(((col+1)*span_w+col)*bottle_w), 3), dtype="uint8")*250# 起始点
stx = span_w * bottle_w
sty = bottle_h# 画瓶子
for c in range(col):for r in range(row):stx2 = int(stx + (1+span_w)*bottle_w*c)sty2 = int(sty + (1+span_h)*bottle_h*r)print(stx2, sty2)for i in range(0, bottle_h-1):for j in range(0, bottle_w-1):background2[sty2+i, stx2+j] = bottle2[i, j]
写字
在OpenCV中无法直接写入中文,所以使用 PIL,具体步骤参看
https://www.cnblogs.com/aidenzdly/articles/10789829.html
其中 ttf 文件为字体文件,可在百度中查询下载
因为写字时要进行图片格式转换 Image.fromarray ,所以无法再上一个循环中写字,再加一套循环
# 写字
fontpath = "zi.ttf"
font = ImageFont.truetype(fontpath, 20)
img_pil = Image.fromarray(background2)
draw = ImageDraw.Draw(img_pil)num = 0
for c in range(col):for r in range(row):stx2 = int(stx + (1+span_w)*bottle_w*c)sty2 = int(sty + (1+span_h)*bottle_h*r)# background2 = cv.putText(background2, writ[num], (stx2+int(0.1*bottle_w), sty2+int(1.3*bottle_h)), cv.FONT_HERSHEY_COMPLEX, 0.8, (0,0,0), 2)draw.text((stx2+int(0.2*bottle_w), sty2+int(1.2*bottle_h)), writ[num], font = font, fill = (0,0,0))num += 1font = ImageFont.truetype(fontpath, 30)
draw.text((int(0.4*background2.shape[1]), int(0.5*bottle_h)), title, font = font, fill = (0,0,0))bk_img = np.array(img_pil)cv.imshow("add_text",bk_img)
cv.waitKey(0)
效果
全套代码
# -*- coding: utf-8 -*-
# @Author: zhr
# @Date: 2020-02-19 21:15:26
# @Last Modified by: zhr
# @Last Modified time: 2020-02-19 23:44:52
import cv2 as cv
import numpy as np
from PIL import ImageFont, ImageDraw, Image# 导入瓶子并滤波
bottle = cv.imread("bottle.jpg")
bottle2 = cv.fastNlMeansDenoisingColored(bottle)# 输入参数
row = int(input("行:"))
col = int(input("列:"))
title = input("题目:")
print("输入文字: ")
writ = []
for i in range(row*col):zi = input("{}:".format(i+1))writ.append(zi)# 瓶子左右间距、上下间距
span_w = 0.4
span_h = 0.8(bottle_h, bottle_w, _) = bottle2.shapebackground2 = np.ones((int((row+row*span_h-span_h+2)*bottle_h), int(((col+1)*span_w+col)*bottle_w), 3), dtype="uint8")*250# 起始点
stx = span_w * bottle_w
sty = bottle_h# 画瓶子
for c in range(col):for r in range(row):stx2 = int(stx + (1+span_w)*bottle_w*c)sty2 = int(sty + (1+span_h)*bottle_h*r)print(stx2, sty2)for i in range(0, bottle_h-1):for j in range(0, bottle_w-1):background2[sty2+i, stx2+j] = bottle2[i, j]# 写字
fontpath = "zi.ttf"
font = ImageFont.truetype(fontpath, 20)
img_pil = Image.fromarray(background2)
draw = ImageDraw.Draw(img_pil)num = 0
for c in range(col):for r in range(row):stx2 = int(stx + (1+span_w)*bottle_w*c)sty2 = int(sty + (1+span_h)*bottle_h*r)# background2 = cv.putText(background2, writ[num], (stx2+int(0.1*bottle_w), sty2+int(1.3*bottle_h)), cv.FONT_HERSHEY_COMPLEX, 0.8, (0,0,0), 2)draw.text((stx2+int(0.2*bottle_w), sty2+int(1.2*bottle_h)), writ[num], font = font, fill = (0,0,0))num += 1font = ImageFont.truetype(fontpath, 30)
draw.text((int(0.4*background2.shape[1]), int(0.5*bottle_h)), title, font = font, fill = (0,0,0))bk_img = np.array(img_pil)cv.imshow("add_text",bk_img)
cv.waitKey(0)
好了,可以发给女朋友,快乐涂色了
Python OpenCV 自动生成快乐源泉小瓶子图片(OpenCV中文写字)相关推荐
- Python 自动生成快乐源泉小瓶子(智能版)
目标 通过Python语言和相关技术库实现 快乐源泉小瓶子的自动生成. 程序文件 包括Python代码文件 main.py, bottle.json生成小瓶子的配置信息文件,以及bottle.jpg小 ...
- 50行python代码自动生成文章
不知道从小到大,我们被迫写了多少心得体会,多少人生感想,如果真情实地的去感受写作然后成长当然很好,但是更多的都是形式主义的需求,并没有人去看里面的内容,白白浪费我们多少大好时光,有时候我们ctrl,C ...
- python生成yaml_使用python脚本自动生成K8S-YAML的方法示例
1.生成 servie.yaml 1.1.yaml转json service模板yaml apiVersion: v1 kind: Service metadata: name: ${jarName} ...
- python生成yaml_使用python脚本自动生成K8S-YAML
使用python脚本自动生成K8S-YAML 1.生成 servie.yaml 1.1.yaml转json service模板yaml apiVersion: v1 kind: Service met ...
- Python爬取不羞涩网小姐姐图片——BeautifulSoup应用
引言 今年提倡原地过年,相信很多朋友都没有回家过年,像我就被迫留在深圳过年了,无聊之余只能去看看电影爬爬山.今天给大家带来一个打发无聊时光的案例,用Python爬取不羞涩网小姐姐图片,并保存到本地,老 ...
- Python三步爬取VMgirls小姐姐图片
Python三步爬取VMgirls小姐姐图片 具体思路 第一步:确定目标 第二步:分析目标网站 第三步:代码编写 具体思路 第一步:确定目标:寻找目标网站,我选择的网站是http://www.VMgi ...
- WordPress彻底禁用上传媒体图片自动生成缩略图及多尺寸图片(亲测可用)
WordPress默认上传图片的时候会自动生成缩略图及多尺寸的图片文件,大部分网站都用不到这些多余的图片,不仅仅占用空间,而且上传的时候还会消耗额外的性能. 下面仅需两段函数代码即可彻底禁用该功能. ...
- 简单的二维码生成接口,自动生成二维码,返回图片地址
自动生成二维码,返回图片地址 本来不想写的,但是怕太久不写这个东西,就要荒废了,就先记录一下简单的东西 这里因为,返回地址的时候,通过了nginx ,我试了很多方法都抓取不到对应的IP地址, 就在ng ...
- python 文案自动生成_Python自动化测试如何自动生成测试用例?
原文作者:陈安妮annie1 原出处:简书 上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰,我们将立即处理. 传统的测试用例需要测试或者开发人员将用户的操作用代码表示出来,通过断言判断 ...
最新文章
- 如何挑选靠谱的Java培训机构
- SAP RETAIL 如何查看分配表是参考哪个PO来创建的?
- boost::multiprecision模块将 std::numeric_limits 用作 multiprecision.qbk 上的多精度文档片段的示例
- php中ajax用法,thinkphp中使用ajax
- cpu超频软件_AMD 锐龙7 3700X(默频)全面对决i7-9700K(超频至5.0GHz)
- MyBatis防止SQL注入的方法
- 主流数据库对比,主流数据库性能、选型对比
- treetable怎么带参数_jquery treeTable插件使用细则
- 弘辽科技:淘宝开店类别怎么选择?淘宝开店如何选类目?
- 解决du df结果不一样的问题
- 七夕活动主题html邮件,七夕节活动策划方案,七夕创意活动主题
- 用GNS3制作路由交换网络拓扑图
- 【明日方舟 人工智能】在罗德岛学习人工智能的日子 (一)
- 三重积分的轮换对称性及极坐标形式确定上下限
- 工业相机镜头的参数与选型
- Idea关于Module is not backed by gradle的问题(部分转)
- python适合什么发型-下面不属于python特性的是( )。
- 照片怎么无损放大尺寸,三种方法无损放大照片
- python turtle画樱花树
- vue + echarts 之世界地图
热门文章
- 金山卫士开源软件之旅(十) KSafeMain工程的分析 1
- 2022/10/17-10/22周报
- python dataframe去掉索引_python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)...
- 人工智能神经网络有哪些研究的领域,详细说明
- 书呆子rico_我如何在StreetComplete任务中度过一个书呆子约会之夜
- 【总结】线性代数的本质 - 2
- Python实时检测文件及文件夹变动
- 儿童磁铁玩具,磁性积木片CPC认证,ASTM F963、CPSIA测试
- 怎么退出用户登录linux,linux如何退出用户
- 【融职培训】Web前端学习 第2章 网页重构11 HTML5新增标签