代码如下:

import osimport cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image# 生成空白文件
def new_image(width, height, color, name):img = Image.new('RGB', (width, height), (color["r"], color["g"], color["b"]))# img.show()img.save(name)# 创建字体文件
def creat_font_img(value, file_name, path):# 编辑图片路径img = cv2.imread(path)# 设置需要显示的字体fontpath = "font/simsun.ttc"# 32为字体大小font = ImageFont.truetype(fontpath, 32)img_pil = Image.fromarray(img)draw = ImageDraw.Draw(img_pil)# 获取字体宽度sum_width = 0sum_height = 0for char in value:width, height = draw.textsize(char, font)sum_width += widthsum_height = height# 绘制文字信息# (100,300/350)为字体的位置,(255,255,255)为白色,(0,0,0)为黑色draw.text(((img.shape[1] - sum_width) / 2, (img.shape[0] - sum_height) / 2), value, font=font, fill=(0, 0, 0))bk_img = np.array(img_pil)# cv2.imshow("add_text", bk_img)cv2.waitKey()  # 保存图片路径cv2.imwrite(file_name + "_font.png", bk_img)def image_resize(img, size=(1500, 1100)):"""调整图片大小"""try:if img.mode not in ('L', 'RGB'):img = img.convert('RGB')img = img.resize(size)except Exception as e:passreturn img# 合并图片(垂直)
def image_merge(images, output_dir='./', output_name='merge.jpg', \restriction_max_width=None, restriction_max_height=None):"""垂直合并多张图片images - 要合并的图片路径列表ouput_dir - 输出路径output_name - 输出文件名restriction_max_width - 限制合并后的图片最大宽度,如果超过将等比缩小restriction_max_height - 限制合并后的图片最大高度,如果超过将等比缩小"""max_width = 0total_height = 0# 计算合成后图片的宽度(以最宽的为准)和高度for img_path in images:if os.path.exists(img_path):img = Image.open(img_path)width, height = img.sizeif width > max_width:max_width = widthtotal_height += height# 产生一张空白图new_img = Image.new('RGB', (max_width, total_height), 255)# 合并x = y = 0for img_path in images:if os.path.exists(img_path):img = Image.open(img_path)width, height = img.sizenew_img.paste(img, (x, y))y += heightif restriction_max_width and max_width >= restriction_max_width:# 如果宽带超过限制# 等比例缩小ratio = restriction_max_height / float(max_width)max_width = restriction_max_widthtotal_height = int(total_height * ratio)new_img = image_resize(new_img, size=(max_width, total_height))if restriction_max_height and total_height >= restriction_max_height:# 如果高度超过限制# 等比例缩小ratio = restriction_max_height / float(total_height)max_width = int(max_width * ratio)total_height = restriction_max_heightnew_img = image_resize(new_img, size=(max_width, total_height))if not os.path.exists(output_dir):os.makedirs(output_dir)save_path = '%s/%s' % (output_dir, output_name)new_img.save(save_path)return save_pathcolor = {}
color["r"] = 255
color["g"] = 255
color["b"] = 255
new_image(430, 100, color, "a.jpg")
creat_font_img("aaaaaaa", "a", "a.jpg")
creat_font_img("好好好", "b", "a.jpg")
creat_font_img("emmmmmmmm", "c", "a.jpg")
image_merge(images=['a_font.png', 'b_font.png', 'c_font.png'], output_name='mer.png')

具体大家可以自行尝试……

Python创建文字图片(居中)/多图片合并(PIL)相关推荐

  1. CSDN图片居中、图片/表格添加题注

    1.图片居中 在图片的地址末尾加入#pic_center即可.如 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200519092936637.png?x-os ...

  2. css垂直居中怎么设置?文字上下居中和图片垂直居中

    css 居中分css垂直居中和css水平居中,水平居中平时比较常用,这里我们主要讲css上下居中的问题.垂直居中又分为css文字上下居中和css图片垂直居中,下面我们就分别来介绍一下. css文字上下 ...

  3. html5核心ml5图片居中,HTML5图片居中的问题

    刚开始接触html5,准备写一些网页,但是学习的过程中遇到了图片不能居中的问题,首先来看看,代码和执行效果: 1 DOCTYPE html> 2 3 4 5 6 7 图片居中问题title> ...

  4. php图片居中显示图片,怎样让css图片居中显示

    在项目中我们也经常会遇到这种情况,要让图片居中显示,方法有很多,不过最推荐的就是用CSS来操作DIV,那么下面就给大家举介绍俩种方法,用CSS操作让图片居中显示. 首先让图片中DIV对象盒子内水平居中 ...

  5. html中div设置图片居中显示,图片在DIV中居中显示的方法

    问:如何让图片在DIV中水平和垂直两个方向都居中?(注意这里的图片非用作背景) 关于该问题的解决方法如下: 方法一: 思路:利用text-align属性将图片水平居中,然后设置padding-top的 ...

  6. python创建一个有序链表_Python实现合并两个有序链表的方法示例

    本文实例讲述了Python实现合并两个有序链表的方法.分享给大家供大家参考,具体如下: 思路:先选出第一个节点,然后遍历两个链表,把小的作为当前节点的下一个节点,一直到其中一个链表遍历完,这时候把另一 ...

  7. Markdown——图片、文字显示居中的一种方法

    Markdown--图片.文字显示居中的一种方法 一.方法 1.1 图片居中 1.2 文字居中 二.图片居中举例 2.1 编辑界面 2.2 显示界面 一.方法 1.1 图片居中 在图片下面的链接的最后 ...

  8. 转python 将base64字符串还原成图片保存

    转python 将base64字符串还原成图片保存 import os,base64 strs='''/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLC ...

  9. dw图片居中怎么设置_【面试前】css样式垂直/水平/垂直水平居中的方式总结

    所有样式设置之前,Body元素可能会自带一个margin属性,需要将其覆盖掉.否则有些方法使用时可能会导致窗口边缘出现滚动条. 所以开始之前需要: 水平居中 1. 父元素设置text-align:ce ...

最新文章

  1. python中rename函数_python os.rename(…)不起作用!
  2. es java match_ES multi_match 和match查询
  3. springboot 1.5.2 集成kafka 简单例子
  4. 用VBA得到EXCEL表格中的行数和列数
  5. rabbitmq订单模块_RabbitMQ播放模块! 构架
  6. 【OpenCV】傅里叶变换
  7. Leetcode 110.平衡二叉树
  8. bzoj1088: [SCOI2005]扫雷Mine
  9. .Net中的并行编程-6.常用优化策略
  10. 谭浩强《C语言程序设计第五版》第一章学习笔记
  11. 轻松掌柜显示不能连接到服务器,轻松掌柜推出在线进销存云端版
  12. 安装.NET Framework 4失败解决办法
  13. Axure原型设计介绍
  14. 单龙芯3A3000-7A1000PMON研究学习-(17)撸起袖子干-分析代码前的准备工作4-地址映射
  15. No MyBatis mapper was found in ‘[xx.mapper]‘ package. Please check your configuration.特殊处理
  16. 专访智齿科技徐懿丨企服公司四要素:智能、融合、产品复杂度、客单价
  17. Endnote使用笔记01:如何在Endnote 20中导入GB/T7714-2015引文格式
  18. vue父子组件传值:详解父组件向子组件传值(props)
  19. dev c++播放音乐MP3(win环境)亲测有效
  20. IOS 移动端视频播放不全屏

热门文章

  1. oracle purge作业,Oracle Purge和drop的区别
  2. JavaScript undefined
  3. Java8 时间转换器
  4. 34寸显示器作Mac外接怎么用才最爽
  5. dcl并列控件 lisp_CAD autolisp jjj-dcl-make函数
  6. 免费域名 免费空间 免费主页空间 免费动态主页空间
  7. 【最快的ubuntu系统安装微信教程】
  8. linux进阶-sudo命令的理解
  9. 判断NP完全问题的一些蛛丝马迹
  10. java个人小管家代码_java毕业设计_springboot框架的家庭小管家系统