python实现图片插入文字
作者:staHuri 时间:2021-11-02 20:32:09
本文实例为大家分享了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()
来源:https://blog.csdn.net/staHuri/article/details/83822364
标签:python,图片,插入文字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用python list完成最简单的DB连接池方法
2022-04-19 18:38:00
![](https://img.aspxhome.com/file/2023/9/95319_0s.jpg)
利用Python写个摸鱼监控进程
2022-11-04 10:40:45
![](https://img.aspxhome.com/file/2023/3/115133_0s.jpg)
python字符串连接的N种方式总结
2023-10-12 08:47:44
Python+requests+unittest执行接口自动化测试详情
2023-07-30 15:08:37
![](https://img.aspxhome.com/file/2023/5/65125_0s.png)
Python self用法详解
2021-08-24 04:26:41
asp如何选择访问速度最快的站点?
2010-06-10 18:34:00
保护MySQL数据库中重要数据的注意事项
2009-01-19 11:55:00
Python之lambda匿名函数及map和filter的用法
2021-01-14 02:11:18
python怎么判断素数
2021-09-30 11:10:33
Python中Numpy和Matplotlib的基本使用指南
2021-10-26 04:22:44
![](https://img.aspxhome.com/file/2023/6/112926_0s.png)
Python3自动安装第三方库,跟pip说再见
2022-03-12 04:34:15
![](https://img.aspxhome.com/file/2023/6/76896_0s.png)
在oracle 数据库中查看一个sql语句的执行时间和SP2-0027错误
2009-10-09 13:04:00
django框架实现一次性上传多个文件功能示例【批量上传】
2022-03-30 09:00:43
![](https://img.aspxhome.com/file/2023/7/66767_0s.png)
完美解决ARIMA模型中plot_acf画不出图的问题
2023-07-13 14:17:34
![](https://img.aspxhome.com/file/2023/1/61761_0s.jpg)
Python使用jpype模块调用jar包过程解析
2023-04-12 03:07:12
pybind11和numpy进行交互的方法
2021-08-18 23:24:14
多个python文件调用logging模块报错误
2021-03-30 23:30:24
Python机器学习之K-Means聚类实现详解
2022-01-20 07:05:14
![](https://img.aspxhome.com/file/2023/8/115408_0s.jpg)
python对绑定事件的鼠标、按键的判断实例
2021-05-20 03:12:58
Python dict和defaultdict使用实例解析
2022-12-24 20:44:19
![](https://img.aspxhome.com/file/2023/1/80631_0s.png)