python将文本转换成图片输出的方法
作者:feiwen 时间:2023-06-20 11:47:47
本文实例讲述了python将文本转换成图片输出的方法。分享给大家供大家参考。具体实现方法如下:
#-*- coding:utf-8 -*-
from PIL import Image,ImageFont,ImageDraw
text = u'欢迎访问脚本之家,https://www.jb51.net'
font = ImageFont.truetype("msyh.ttf",18)
lines = []
line =''
for word in text.split():
print word
if font.getsize(line+word)[0] >= 300:
lines.append(line)
line = u''
line += word
print 'size=',font.getsize(line+word)[0]
else:
line = line + word
line_height = font.getsize(text)[1]
img_height = line_height*(len(lines)+1)
print 'len=',len(lines)
print 'lines=',lines
im = Image.new("RGB",(444,img_height),(255,255,255))
dr = ImageDraw.Draw(im)
x,y=5,5
for line in lines:
dr.text((x,y),line,font=font,fill="#000000")
y += line_height
im.save("1.1.jpg")
希望本文所述对大家的Python程序设计有所帮助。
标签:python,文本,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python字典中items()函数案例详解
2021-02-23 04:47:31
pytorch使用 to 进行类型转换方式
2022-10-05 23:28:27
![](https://img.aspxhome.com/file/2023/6/101576_0s.jpg)
分享13款非常有用的jQuery插件
2011-05-16 19:07:00
Python 专题二 条件语句和循环语句的基础知识
2022-11-29 16:46:41
![](https://img.aspxhome.com/file/2023/3/107613_0s.jpg)
给年青设计师们的十条经验教训
2011-03-31 17:09:00
十幅图告诉你什么是PHP引用
2023-10-04 06:16:56
![](https://img.aspxhome.com/file/2023/0/55770_0s.png)
Python+Django+MySQL实现基于Web版的增删改查的示例代码
2023-11-20 02:09:06
![](https://img.aspxhome.com/file/2023/2/64252_0s.png)
python数据可视化的那些操作你了解吗
2023-11-04 15:18:55
![](https://img.aspxhome.com/file/2023/8/76908_0s.png)
python docx 中文字体设置的操作方法
2021-01-12 22:33:44
SQL语句分组获取记录的第一条数据的方法
2024-01-12 17:23:13
![](https://img.aspxhome.com/file/2023/4/94414_0s.jpg)
实例讲解实现SQL下的字符串拆分具体方法
2008-12-09 14:55:00
Python 读取千万级数据自动写入 MySQL 数据库
2023-11-08 09:25:47
![](https://img.aspxhome.com/file/2023/0/63600_0s.jpg)
Matplotlib条形图之分组条形图和堆叠条形图详解
2021-08-10 15:16:24
![](https://img.aspxhome.com/file/2023/5/124185_0s.png)
php实现的支持断点续传的文件下载类
2023-11-23 16:52:19
FrontPage2002简明教程五:css样式表的应用
2008-09-17 11:31:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017113345609s.jpg)
python批量下载图片的三种方法
2023-08-23 00:00:05
利用SQL Server触发器实现表的历史修改痕迹记录
2024-01-19 18:06:42
![](https://img.aspxhome.com/file/2023/7/99947_0s.jpg)
Go使用Gin+mysql实现增删改查的详细实例
2024-01-20 20:38:43
![](https://img.aspxhome.com/file/2023/6/133996_0s.png)
Python操作SQLite简明教程
2021-08-15 14:32:57
PHP学习记录之面向对象(Object-oriented programming,OOP)基础【类、对象、继承等】
2023-11-21 07:25:59
![](https://img.aspxhome.com/file/2023/0/106310_0s.png)