python的pillow给图片加文字_python PIL(pillow)图像处理-图片上添加文字

此篇博客介绍了如何使用Python的PIL库进行图像操作,包括创建RGBA图像、绘制表格,以及添加文字。通过实践步骤,读者将学会如何打开图片、获取尺寸、设置字体和颜色,并在图片上添加自定义文本。适合初学者和Python图像处理爱好者。
摘要由CSDN通过智能技术生成

from PIL import Image, ImageDraw, ImageFont

def gen_img(size=None):

if size is None:

size = 400

#生成大小为400x400RGBA是四通道图像,RGB表示R,G,B三通道,A表示Alpha的色彩空間

image = Image.new(mode='RGBA', size=(400, 400), color=(255, 55, 55))

# ImageDraw.Draw 简单平面绘图

draw_table = ImageDraw.Draw(im=image)

# 直接显示图片

image.show()

def pic_open(filepath):

#图片打开与显示

image = Image.open(filepath)

return image

def get_size(image):

#获取图像的宽和高

width, height = image.size

return width, height

def pic_text(filepath,size,text,setFont,fillColor,filename,direction=None):

print(filepath,size,text,setFont,fillColor)

#打开图片

image=pic_open(filepath)

#新建绘图对象

draw = ImageDraw.Draw(image)

#显示图片

image.show()

draw.text((40,40),text,font=setFont,fill=fillColor,direction=None)

image.show()

#保存

pic_save(image,filename)

def pic_save(image,filename):

#保存

image.save(filename)

if __name__=="__main__":

size=None

#gen_img()

#** ImageFont模块**

#选择文字字体和大小

setFont = ImageFont.truetype('C:/windows/fonts/Dengl.ttf', 20)

#设置文字颜色

fillColor = "#0000ff" #蓝色

text="兔子等着瞧"

size=(40,40)

filepath="F:/temp/red.png"

filename="F:/temp/redsave.png"

#打开图片

image=pic_open(filepath)

#添加文字

pic_text(filepath,size,text,setFont,fillColor,filename,direction=None)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值