博客动态
Hi!欢迎来到Jerry Coding!公众号垃圾分类增加学习功能,关注下面的公众号一起来帮助Jerry学习更多垃圾分类知识吧!
微信扫码

扫一扫关注我

More
关于
New
RSS

pillow 实现壁纸插入文字处理

2019年7月15日 Jerry 1020 2019年7月15日

       一个简单的小程序,可以实现读取文本,并将文本内容插入到指定的壁纸上。之前用来做了几个备忘壁纸,在这里记一下。

功能:

       读取文本文件,将内容插入到壁纸中。

代码:

#coding:utf-8
from PIL import Image, ImageDraw, ImageFont

# 背景图
bg = './bg.png'
out_img = './out.png'

base = Image.open(bg).convert('RGBA')

# 指定图片尺寸等
txt = Image.new('RGBA', base.size, (255,255,255,0))

# 设置字体
fnt = ImageFont.truetype('./simsun.ttc', 36)

# 文本插入
d = ImageDraw.Draw(txt)

#读取文件
with open('helloworld.txt', 'r') as fp:
    s = fp.read()

# 字体写入 指定偏移 x=0 y=100   颜色(255,255,255,255)
d.text((0,200), s, font=fnt, fill=(255,255,255,255))

out = Image.alpha_composite(base, txt)
out.save(out_img)
out.show()

效果:

扩展:

       更多的Pillow操作(分享、截屏、剪切板等等),可以参考这个:CSDN博客


原创文章,转载请注明出处: https://jerrycoding.com/article/pillow-demo

微信
支付宝

您尚未登录,暂时无法评论。请先 登录 或者 注册

0 人参与 | 0 条评论