python PIL模块在图片画线写字

图片上画线条

import sys
from PIL import Image,ImageDraw

im = Image.open("th.png")
draw = ImageDraw.Draw(im) #实例化一个对象
draw.line((0, 0) + im.size, fill=128, width=5)  #线的起点和终点,线宽
draw.line((0, im.size[1], im.size[0], 0), fill=128)
draw.line((0,im.size[1]/2)+(im.size[0]/2,im.size[1]), fill=128, width=5)
im.show()

图片上写字

from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open('th.jpg').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('cambriai.ttf', 40)
# get a drawing context
d = ImageDraw.Draw(txt)
# draw text, half opacity
d.text((10,10), "Hello", font=fnt, fill=(255,255,255,128))
# 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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值