PIL篇---python 实现PIL模块在图片画线写字

python 实现PIL模块在图片画线写字

from PIL import Image, ImageDraw, ImageFont


# get an image
base = Image.open('2022-07-19-09-48-05.jpeg').convert('RGBA')
# make a blank image for the text, initialized to transparent text color
txt = Image.new('RGBA', base.size, (255,255,255,0))
# get a font 需要在C:\Windows\Fonts拷贝一份字体文件 当前脚本路径下
fnt = ImageFont.truetype('simhei.ttf', 40)
# get a drawing context
d = ImageDraw.Draw(txt)

# 需要修改 box的位置
box = tuple((1203,694,1339,867))
d.rectangle(box, fill=None, outline="red", width=5)
# draw text, half opacity  (255,0,255,255) 其中(255,0,255)是颜色,最后的255 是深浅
d.text((1203,654), "label 0.98", font=fnt, fill=(255,0,255,255))
# draw text, full opacity
# d.text((10,60), "World", font=fnt, fill=(255,255,255,255))
fillcolor = "#ff0000"  #字体颜色
d.text((base.size[0]-20,10), "4", font=fnt, fill=fillcolor)
out = Image.alpha_composite(base, txt)
out.show()

参考官方文档
https://pillow.readthedocs.io/en/stable/reference/Image.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心惠天意

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值