目标

通过Python语言和相关技术库实现 快乐源泉小瓶子的自动生成。

程序文件

包括Python代码文件 main.py, bottle.json生成小瓶子的配置信息文件,以及bottle.jpg小瓶子图像,bottle2.jpg生成后的快乐源泉小瓶子

bottle.jpg

从网上找的小瓶子图像

bottle.json 配置文件

{"title":"快乐源泉小瓶子",  "bottle_path":"./bottle.jpg","list":[["第一个","第二个","第三个","第四个","第五个","第六个","第七个","第八个"],["第一个","第二个","第三个","第四个","第五个","第六个","第七个"],["第一个","第二个","第三个","第四个","第五个","第六个","第七个","第八个"],["第一个","第二个","第三个","第四个","第五个","第六个","第七个"],["第一个","第二个","第三个","第四个","第五个","第六个","第七个","第八个"]],"font_size":120,"fontpath":"C:\\WINDOWS\\FONTS\\MSYHL.TTC"
}

其中  title 是标题, bottle_path 是小瓶子路径,   font_size 是 标签瓶子字体大小, fontpath是字体路径,想用什么可以自行更换。

list是最重要的,是想生成瓶子的标签列表,按照这个格式替换即可,注意要是字符性状,标签不能太长。

main.py

最关键的代码部分,

环境支持

import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image
import json

配置参数读取设置

读取json文件中设置的相关参数,读取瓶子图像等...

with open('bottle.json', 'r', encoding='utf-8') as fp:bottle_configure = json.load(fp)title=bottle_configure['title']
bottle_list=bottle_configure['list']
fontpath=bottle_configure['fontpath']
font_size=bottle_configure['font_size']
bottle_path=bottle_configure['bottle_path']#字体设置
font = ImageFont.truetype(fontpath, font_size)
font_title=ImageFont.truetype(fontpath, int(font_size*2.5))#读取瓶子图像
bottle=cv2.imread(bottle_path)
#可在此处自行添加滤波操作(可选) bottle2=.....
bottle2=bottle# 瓶子左右间距、上下间距
span_w = 0.4
span_h = 0.8

行列计算、总体图像大小、第一个瓶子起始点设置

根据输入的list 自动判断行数和列数,根据输入的小瓶子图像和计算的行数,列数生成相应的幕布(用于绘制图像和文字),并设置第一个瓶子的起始点。

row=len(bottle_list)
col=0
#计算最大列
for l in bottle_list:if len(l)>col:col=len(l)(bottle_h, bottle_w, _) = bottle2.shapebackground = np.ones((int((row+row*span_h-span_h+2+1)*bottle_h), int(((col+1)*span_w+col)*bottle_w), 3), dtype="uint8")*250# 起始点
stx = span_w * bottle_w
sty = bottle_h*2

绘制

#背景色一致
Background_color=np.mean(bottle2[0:3,0:3,0:3])
(background_h,background_w,_)=background.shape
for i in range(0,background_h-1):for j in range(0,background_w-1):background[i,j]=Background_color# 绘制瓶子
for r in range(row):for c in range(min(col,len(bottle_list[r]))):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):background[sty2+i, stx2+j] = bottle2[i, j]#绘制文字
img_pil = Image.fromarray(background)
draw = ImageDraw.Draw(img_pil)
##标题
draw.text((int(0.32*background_w), int(0.5*bottle_h)),  title, font = font_title, fill = (0,0,0))
##标签
for r in range(row):for c in range(min(col,len(bottle_list[r]))):stx2 = int(stx + (1+span_w)*bottle_w*c)sty2 = int(sty + (1+span_h)*bottle_h*r)# background = cv.putText(background, bottle_list[num], (stx2+int(0.1*bottle_w), sty2+int(1.3*bottle_h)), cv.FONT_HERSHEY_COMPLEX, 0.8, (0,0,0), 2)string=bottle_list[r][c]string_len=len(string)print(string,string_len)#根据字数调整绘制的位置if string_len<=2:draw.text((stx2+int(0.25*bottle_w), sty2+int(1.2*bottle_h)),  bottle_list[r][c], font = font, fill = (0,0,0))elif string_len==3:draw.text((stx2+int(0.14*bottle_w), sty2+int(1.2*bottle_h)),  bottle_list[r][c], font = font, fill = (0,0,0))elif string_len==4:draw.text((stx2+int(0.03*bottle_w), sty2+int(1.2*bottle_h)),  bottle_list[r][c], font = font, fill = (0,0,0))else:draw.text((stx2+int(-0.10*bottle_w), sty2+int(1.2*bottle_h)),  bottle_list[r][c], font = font, fill = (0,0,0))

完整代码

import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image
import json with open('bottle.json', 'r', encoding='utf-8') as fp:bottle_configure = json.load(fp)title=bottle_configure['title']
bottle_list=bottle_configure['list']
fontpath=bottle_configure['fontpath']
font_size=bottle_configure['font_size']
bottle_path=bottle_configure['bottle_path']font = ImageFont.truetype(fontpath, font_size)
font_title=ImageFont.truetype(fontpath, int(font_size*2.5))bottle=cv2.imread(bottle_path)
#滤波
# bottle2 = .....(bottle)
bottle2=bottle# 瓶子左右间距、上下间距、行数、列数
span_w = 0.4
span_h = 0.8row=len(bottle_list)
col=0
#计算最大列
for l in bottle_list:if len(l)>col:col=len(l)
print(col)(bottle_h, bottle_w, _) = bottle2.shape
background = np.ones((int((row+row*span_h-span_h+2+1)*bottle_h), int(((col+1)*span_w+col)*bottle_w), 3), dtype="uint8")*250# 起始点
stx = span_w * bottle_w
sty = bottle_h*2#背景色一致
Background_color=np.mean(bottle2[0:3,0:3,0:3])
(background_h,background_w,_)=background.shape
for i in range(0,background_h-1):for j in range(0,background_w-1):background[i,j]=Background_color# 绘制小瓶子
for r in range(row):for c in range(min(col,len(bottle_list[r]))):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):background[sty2+i, stx2+j] = bottle2[i, j]#绘制文字
img_pil = Image.fromarray(background)
draw = ImageDraw.Draw(img_pil)
##标题
draw.text((int(0.32*background_w), int(0.5*bottle_h)),  title, font = font_title, fill = (0,0,0))
##标签
for r in range(row):for c in range(min(col,len(bottle_list[r]))):stx2 = int(stx + (1+span_w)*bottle_w*c)sty2 = int(sty + (1+span_h)*bottle_h*r)# background = cv.putText(background, bottle_list[num], (stx2+int(0.1*bottle_w), sty2+int(1.3*bottle_h)), cv.FONT_HERSHEY_COMPLEX, 0.8, (0,0,0), 2)string=bottle_list[r][c]string_len=len(string)print(string,string_len)if string_len<=2:draw.text((stx2+int(0.25*bottle_w), sty2+int(1.2*bottle_h)),  bottle_list[r][c], font = font, fill = (0,0,0))elif string_len==3:draw.text((stx2+int(0.14*bottle_w), sty2+int(1.2*bottle_h)),  bottle_list[r][c], font = font, fill = (0,0,0))elif string_len==4:draw.text((stx2+int(0.03*bottle_w), sty2+int(1.2*bottle_h)),  bottle_list[r][c], font = font, fill = (0,0,0))else:draw.text((stx2+int(-0.10*bottle_w), sty2+int(1.2*bottle_h)),  bottle_list[r][c], font = font, fill = (0,0,0))bk_img = np.array(img_pil)
cv2.imwrite('./bottle2.jpg',bk_img)
cv2.waitKey(0)

代码有疑问的地方可以私信/留言,收到后我会补充。  如果支持的人多,会更新下一版本,让标签文字大小和文字位置自动化、智能化 以及 添加多个瓶子 样式 支持。

谢谢各位。

Python 自动生成快乐源泉小瓶子(智能版)相关推荐

  1. Python OpenCV 自动生成快乐源泉小瓶子图片(OpenCV中文写字)

    目的 空间看到很多小瓶子,大部分是由P图产生的,会影响图片清晰和质量,而且速度较慢.所以打算使用OpenCV自动生成 步骤 瓶子 没有找到单独的瓶子原图,所以截取了一个,有很多的噪点和不清晰,所以Op ...

  2. python 自动生成问卷表的软件的设计与实现 毕业设计源码291138

    摘 要 本论文主要论述了如何使用PHP语言开发一个自动生成问卷表的软件的设计与实现,本系统将严格按照软件开发流程进行各个阶段的工作,采用B/S架构,面向对象编程思想进行项目开发.在引言中,作者将论述自 ...

  3. 用Python自动生成NBA巨星生涯数据曲线

    1.序 之前写过一个用 python 自动生成球员职业生涯数据的程序(原文请关注本人公众号),大家的反响很好,我也感到很欣慰.有问我怎么做的,如何学 python 的,也有提建议说集成到 web 里面 ...

  4. python新建word文档_使用Python 自动生成 Word 文档的教程

    当然要用第三方库啦 :) 使用以下命令安装: pip install python-docx 使用该库的基本步骤为: 1.建立一个文档对象(可自动使用默认模板建立,也可以使用已有文件). 2.设置文档 ...

  5. python自动生成和读取word_使用Python自动生成Word文档的教程

    当然要用第三方库啦 :) 使用以下命令安装: pip install python-docx 使用该库的基本步骤为: 1.建立一个文档对象(可自动使用默认模板建立,也可以使用已有文件). 2.设置文档 ...

  6. 情人节到了,用Python自动生成520照片墙吧~

    导语 情人节到了,用Python自动生成520照片墙可好,没有对象的自己看 相关文件 关注微信公众号 Python日志, 公众号内回复'照片墙'获取. 视频教学 视频教学:哔哩哔哩照片墙制作 开发工具 ...

  7. python自动汇总表格_用Python自动生成Excel报表

    作者 / 来源:林骥(ID:linjiwx) 01 安装和导入模块 以 Python 中的 openpyxl 模块为例,它能够读取和修改 Excel 文件,如果你还没有安装,可以通过以下命令进行安装: ...

  8. python ppt自动生成_实战 | Python自动生成PPT调研报告

    原标题:实战 | Python自动生成PPT调研报告 原文: 全文约 3821 字,读完可能需要 5 分钟. 文/JSong @2017.02.28 在数据分析里面有一句话是说,80%的时间要用于数据 ...

  9. python制作相册_《自拍教程73》Python 自动生成相册文件夹

    这里将告诉您<自拍教程73>Python 自动生成相册文件夹,具体操作过程:案例故事: 接Python mediainfo批量重命名图片文件,测试图片是批量重命名好了, 但是将测试图片放于 ...

最新文章

  1. 欢迎大家观顾【图灵教育社区】
  2. 仿快图系统自带图片浏览器应用源码项目
  3. Beginning iCloud in iOS 5 Tutorial Part 2(转载)
  4. Python - SIP参考指南 - 介绍
  5. 深度学习《CNN架构续篇 - 学习率衰减》
  6. python win7 win10_Python如何获取Win7,Win10系统缩放大小
  7. 当开源奔向物流,阿里云 PolarDB-X 数据库与韵达携手的背后
  8. 【操作系统】输入输出系统(下)-思维导图
  9. win11u盘安装报错怎么办 windows11u盘安装报错的解决方法
  10. WCF问题:“HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理”解决方法...
  11. tensor.detach() 和 tensor.data 的区别
  12. nginx域名反向代理
  13. 图像修复神器!带上口罩都能还原!DDPM:用去噪扩散概率模型极限修复图像,效果太牛了!...
  14. 【模电】第十章、信号处理与信号产生电路(振荡电路)
  15. matlab主成分分析散点图_主成分分析(PCA)及其在MATLAB中的实现
  16. webmail 客户端_开源Webmail客户端Isotope入门
  17. android x86 修改器,烧饼修改器x86专属版
  18. Zhong__安装配置ElasticSearch
  19. 反三角函数在matlab中怎样定义
  20. {}怎样进行邮件推广

热门文章

  1. 使用Qt实现计算器功能
  2. 原来何恺明提出的MAE还是一种数据增强
  3. 服务器异常流量导致性能下降怎么解决,云服务器查看异常流量
  4. 三天卖光千亩顶级玫瑰,聚划算如何将品质性价比做到极致?
  5. android alarmmanager定时任务,AlarmManager 定时任务详解
  6. 如何通过SEO搜索引擎关键词优化获客?
  7. org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
  8. 2019考研 报名条件是什么?
  9. 多人视频相亲交友源码打破传统相亲
  10. postgresql 表文件介绍