Python Pillow – 在图像上书写文字
在这篇文章中,我们将看到如何用Python Pillow模块在图片上写文字。
安装
这个模块没有预装在Python中。所以要安装它,在命令行中执行以下命令。
pip install pillow
一步一步实现:
步骤1:导入Pillow库
为了完成这项任务,需要从Pillow库中获得的函数是:。Image、ImageDraw、ImageFont。所有这些函数被导入为。
从PIL中导入Image, ImageDraw, ImageFont。
第2步:打开一个图像
在这一步,我们将导入要添加文本的图片,并使用 “Image.open(‘Image_name’) “打开。在给定的案例中,gfg标志被用来在上面添加文本。图片名称是gfg_logo.jpeg。因此,它被写成。
img = Image.open(‘gfg_logo.jpeg’)
第3步:图像转换
在这一步,我们通过使用 “ImageDraw.Draw(‘用于存储上述步骤中的图像的变量’) “将我们的图像转换成可编辑的格式。在给定的案例中,这被写成。
d1 = ImageDraw.Draw(img)
第4步:字体风格。
这个步骤是可选的。这是为那些希望自己的文本看起来很酷或很时尚的人准备的,因为有人不选择任何字体样式,那么系统就会采用默认的字体样式。首先从https://ttfonts.net/font/1004_Mistral.htm 下载字体样式文件。下载文件后,使用函数ImageFont.truetype(‘adddress_of_font_style’, font_style)。在给定的情况下,这被写成:
myFont = ImageFont.truetype(‘/home/raghav/PycharmProjects/gfg/Mistral.ttf’, 20)
第5步: 渲染文本
这是决定字体整体属性的主要步骤。这被写成
d1.text((65, 10), “Sample text”, fill =(255, 0, 0),font=myFont)
- 起始坐标。Pillow库使用笛卡尔像素坐标系统,左上角为(0,0)。
- 文本。单引号或双引号之间的字符串
- 文本颜色为RGB格式。对于你想要的颜色,你可以到谷歌查询它的RGB颜色代码并使用。
- 字体风格。从谷歌下载字体并使用它。
第6步:显示并保存结果。
最后一步是在屏幕上显示修改后的图像,为此使用了img.show()函数,并使用img.save(“results.jpeg”)来存储图像。
以下是实现情况:
# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
# gfg_logo.jpeg image opened using open
# function and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
# Image is converted into editable form using
# Draw function and assigned to d1
d1 = ImageDraw.Draw(img)
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 20)
# Decide the text location, color and font
d1.text((65, 10), "Sample text", fill =(255, 0, 0),font=myFont)
# show and save the image
img.show()
img.save("results.jpeg")
输出:
修改后的图片
例子1:要改变文字的位置,请在步骤5中改变坐标。
尺寸从(65,100)变为(65,170)。因此,文本的位置下降了,因为从(x,y)坐标开始,y坐标的值增加了,如输出中所示。
# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
# gfg_logo.jpeg image opened using
# open function and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
# Image is converted into editable form using
# Draw function and assigned to d1
d1 = ImageDraw.Draw(img)
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 20)
# Decide the text location, color and font
d1.text((65, 170), "Sample text", fill =(255, 0, 0),font=myFont)
# show and save the image
img.show()
img.save("results.jpeg")
输出:
改变坐标后的图像
例子2:要改变文本的颜色,在步骤5中再次改变。
Pillow的工作有RGB颜色代码(R,G,B),R代表红色,G代表绿色,B代表蓝色。在上述情况下,R和B值为0,G值为255,即最大。因此,文本的颜色变成了绿色,如输出中所示。
# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
# gfg_logo.jpeg image opened using
# open function and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
# Image is converted into editable form using Draw function
# and assigned to d1
d1 = ImageDraw.Draw(img)
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 20)
# Decide the text location, color and font
d1.text((65, 170), "Sample text", fill =(0, 255, 0),font=myFont)
# show and save the image
img.show()
img.save("results.jpeg")
输出:
改变文字颜色后的图像
例子3:要改变文本,在步骤5中改变文本字段。
# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
# gfg_logo.jpeg image opened using open function
# and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
# Image is converted into editable form using Draw
# function and assigned to d1
d1 = ImageDraw.Draw(img)
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 20)
# Decide the text location, color and font
d1.text((65, 170), "Sample text 2", fill=(0, 255, 0), font=myFont)
# show and save the image
img.show()
img.save("results.jpeg")
输出:
改变文本字段后的图像
例子4:要改变文字的大小,请到步骤4,改变大小。
# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
# gfg_logo.jpeg image opened using
# open function and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
# Image is converted into editable form using
# Draw function and assigned to d1
d1 = ImageDraw.Draw(img)
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 30)
# Decide the text location, color and font
d1.text((65, 170), "Sample text", fill = (0, 255, 0),font=myFont)
# show and save the image
img.show()
img.save("results.jpeg")
输出:
改变文字大小后的图像
例子5:要改变文本字体,请到步骤4。
# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
# gfg_logo.jpeg image opened using open function and
# assigned to variable named img
img = Image.open('gfg_logo.jpeg')
# Image is converted into editable form using Draw
# function and assigned to d1
d1 = ImageDraw.Draw(img)
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/00006_44s.ttf', 30)
# Decide the text location, color and font
d1.text((0, 170), "Sample text", fill =(0, 255, 0),font=myFont)
# show and save the image
img.show()
img.save("results.jpeg")
输出:
改变字体风格后的图像