Python在图片中插入大量文字并且自动换行


Posted in Python onJanuary 02, 2019

问题

如何在图片中插入大量文字并且自动换行

效果

原始图

Python在图片中插入大量文字并且自动换行

效果图

Python在图片中插入大量文字并且自动换行

注明

若需要写入中文请使用中文字体

实现方式

from PIL import Image, ImageDraw, ImageFont
class ImgText:
  font = ImageFont.truetype("micross.ttf", 24)
  def __init__(self, text):
    # 预设宽度 可以修改成你需要的图片宽度
    self.width = 100
    # 文本
    self.text = text
    # 段落 , 行数, 行高
    self.duanluo, self.note_height, self.line_height = self.split_text()
  def get_duanluo(self, text):
    txt = Image.new('RGBA', (100, 100), (255, 255, 255, 0))
    draw = ImageDraw.Draw(txt)
    # 所有文字的段落
    duanluo = ""
    # 宽度总和
    sum_width = 0
    # 几行
    line_count = 1
    # 行高
    line_height = 0
    for char in text:
      width, height = draw.textsize(char, ImgText.font)
      sum_width += width
      if sum_width > self.width: # 超过预设宽度就修改段落 以及当前行数
        line_count += 1
        sum_width = 0
        duanluo += '\n'
      duanluo += char
      line_height = max(height, line_height)
    if not duanluo.endswith('\n'):
      duanluo += '\n'
    return duanluo, line_height, line_count
  def split_text(self):
    # 按规定宽度分组
    max_line_height, total_lines = 0, 0
    allText = []
    for text in self.text.split('\n'):
      duanluo, line_height, line_count = self.get_duanluo(text)
      max_line_height = max(line_height, max_line_height)
      total_lines += line_count
      allText.append((duanluo, line_count))
    line_height = max_line_height
    total_height = total_lines * line_height
    return allText, total_height, line_height
  def draw_text(self):
    """
    绘图以及文字
    :return:
    """
    note_img = Image.open("001.png").convert("RGBA")
    draw = ImageDraw.Draw(note_img)
    # 左上角开始
    x, y = 0, 0
    for duanluo, line_count in self.duanluo:
      draw.text((x, y), duanluo, fill=(255, 0, 0), font=ImgText.font)
      y += self.line_height * line_count
    note_img.save("result.png")
if __name__ == '__main__':
  n = ImgText(
    "1234567890" * 5)
  n.draw_text()

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。如果你想了解更多相关内容请查看下面相关链接

Python 相关文章推荐
python使用新浪微博api上传图片到微博示例
Jan 10 Python
利用Python获取赶集网招聘信息前篇
Apr 18 Python
Python使用win32com实现的模拟浏览器功能示例
Jul 13 Python
浅谈Pandas中map, applymap and apply的区别
Apr 10 Python
python 用正则表达式筛选文本信息的实例
Jun 05 Python
python 3.3 下载固定链接文件并保存的方法
Dec 18 Python
解决python super()调用多重继承函数的问题
Jun 26 Python
python文档字符串(函数使用说明)使用详解
Jul 30 Python
Python爬取破解无线网络wifi密码过程解析
Sep 17 Python
pycharm 中mark directory as exclude的用法详解
Feb 14 Python
500行python代码实现飞机大战
Apr 24 Python
python绘图模块之利用turtle画图
Feb 12 Python
python 输出所有大小写字母的方法
Jan 02 #Python
Python高斯消除矩阵
Jan 02 #Python
python遍历小写英文字母的方法
Jan 02 #Python
Python最小二乘法矩阵
Jan 02 #Python
Centos部署django服务nginx+uwsgi的方法
Jan 02 #Python
Python 一句话生成字母表的方法
Jan 02 #Python
使用python将请求的requests headers参数格式化方法
Jan 02 #Python
You might like
php简单判断文本编码的方法
2015/07/30 PHP
PHP常用函数之获取汉字首字母功能示例
2019/10/21 PHP
YII2框架中日志的配置与使用方法实例分析
2020/03/18 PHP
基于PHP实现生成随机水印图片
2020/12/09 PHP
获取HTML DOM节点元素的方法的总结
2009/08/21 Javascript
js中将具有数字属性名的对象转换为数组
2011/03/06 Javascript
jQuery中get()方法用法实例
2014/12/27 Javascript
JavaScript实现数据类型的相互转换
2016/03/06 Javascript
JavaScript的模块化开发框架Sea.js上手指南
2016/05/12 Javascript
温习Javascript基础语法之词法结构
2016/05/31 Javascript
js 只比较时间大小的实例
2017/10/26 Javascript
vue单个组件实现无限层级多选菜单功能
2018/04/10 Javascript
解决v-for中使用v-if或者v-bind:class失效的问题
2018/09/25 Javascript
vue实现计算器功能
2020/02/22 Javascript
js实现小时钟效果
2020/03/25 Javascript
[03:14]DOTA2斧王 英雄基础教程
2013/11/26 DOTA
Python实现Const详解
2015/01/27 Python
详细解析Python当中的数据类型和变量
2015/04/25 Python
Python编程中的for循环语句学习教程
2015/10/14 Python
python算法表示概念扫盲教程
2017/04/13 Python
python下10个简单实例代码
2017/11/15 Python
利用python实现微信头像加红色数字功能
2018/03/26 Python
pytorch在fintune时将sequential中的层输出方法,以vgg为例
2019/08/20 Python
Python编程快速上手——强口令检测算法案例分析
2020/02/29 Python
详解Python yaml模块
2020/09/23 Python
Python:__eq__和__str__函数的使用示例
2020/09/26 Python
HTML5+CSS3网页加载进度条的实现,下载进度条的代码实例
2016/12/30 HTML / CSS
澳大利亚Mocha官方网站:包、钱包、珠宝和配饰
2019/07/18 全球购物
拉丁舞学习者的自我评价
2013/10/27 职场文书
24岁生日感言
2014/01/13 职场文书
法人授权委托书
2014/04/03 职场文书
食品安全宣传标语
2014/06/07 职场文书
先进党员事迹材料
2014/12/24 职场文书
导游词之广西漓江
2019/11/02 职场文书
对Golang中的FORM相关字段理解
2021/05/02 Golang
Springboot集成阿里云OSS上传文件系统教程
2021/06/28 Java/Android