欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python---在图片上添加文字

程序员文章站 2024-01-19 08:06:10
使用PIL在图片上添加文字from PIL import Image, ImageDraw, ImageFont# PIL在图像上添加文本height = 1000width = 500img = Image.new('RGB', size=(width, height), color=(255, 255, 255))# 新建绘图对象draw = ImageDraw.Draw(img)# 选择文字字体和大小setFont = ImageFont.truetype("font/simsun...

使用PIL在图片上添加文字

from PIL import Image, ImageDraw, ImageFont

# 创建空白图像
height = 1000
width = 500
img = Image.new('RGB', size=(width, height), color=(255, 255, 255))
# 新建绘图对象
draw = ImageDraw.Draw(img)
# 选择文字字体和大小
setFont = ImageFont.truetype("font/simsun.ttc", 50)
fillColor = (0, 0, 0)  # 文字颜色:黑色
text = "你好 hello"    # 写入的文本,若要换行,字符串中添加'\n'
pos = (0, 0)          # 文本左上角位置 (离左边界距离, 离上边界距离)
draw.text(pos, text, font=setFont, fill=fillColor)
save_path = 'demo.jpg'
img.save(save_path)

参考博客:
Python在图片上添加文字
python PIL图像处理-图片上添加文字

本文地址:https://blog.csdn.net/qq_41982466/article/details/109645827

相关标签: Python