Python PIL library ImageDraw class analysis

(1) Use PIL.ImageDrw to draw text on the picture

1, import dependent library

from PIL import Image, ImageDraw, ImageFont
import cv2 as cv
import numpy as np

2, Create a picture and draw text on the picture

# 采用Image函数创建一张大小为200×200,颜色为绿色的RGB图像
image = Image.new("RGB", (200, 200), (0, 255, 0))
# 在原始图像的基础上创建一个ImageDraw.Draw()实例
draw = ImageDraw.Draw(image)
# 创建需要绘制的文本
text = 'Hello World'
# 设置绘图参数
# 文本左上角坐标:(10, 10);text: 创建的文本内容;fill:文本的颜色
draw.text((10, 10), text, fill=(255, 255, 255))
# 显示绘制好的图片
image.show()
# 保存绘制好的图片
image.save('Hello-World.png')

Hello_World
3. Draw text on the picture and set
the font. To set the font size, you must use a custom font. The default font cannot change the font size. Pillow supports loading TrueType and OpenType fonts. Download the TrueType font address: TrueType font
address

# 选择图片
img = './dog_cat.jpg'
# 读取图片
image = Image.open(img)
# 创建ImageDraw.Draw()实例
draw = ImageDraw.Draw(image)
# 开始在同一张图片上绘制不同字体大小的文本
y = 10  # 设置文本初始纵坐标
text = 'dog ang cat'
for font_size in range(10, 40, 10):  # 设置不同的字体大小
	# ImageFont.truetype()通过size关键字参数设置字体大小
    font = ImageFont.truetype("./Gidole-Regular.ttf", size=font_size)
    draw.text((10, y), '{}: font_size={}'.format(text, font_size), font=font) 
    y += 30
image.save('./set_fontsize.png')

set font size
4. Draw text on the picture and set the color

image = Image.open(img)
draw = ImageDraw.Draw(image)
# 创建颜色列表
colors = ["green", "blue", "red", "yellow", "purple"]
font = ImageFont.truetype("./Gidole-Regular.ttf", size=20)
text = 'dog and cat'
y = 10
for color in colors:
	# 通过关键字参数fill设置字体颜色
    draw.text((10, y), text , font=font, fill=color)
    y += 35
image.save('set_color.png')

set font color
5. Draw multiple lines of text on the picture

image = Image.open(img)
draw = ImageDraw.Draw(image)  # 创建ImageDraw.Draw()对象
texts = ['Dog', 'Cat', 'Dog and Cat']  # 创建需要绘制的文本
font = ImageFont.truetype('./Gidole-Regular.ttf', size=20)
coord = 10
for text in texts:  # 多行绘制文本
    draw.text((10, coord), text, fill=(255, 0, 0), font=font)  # 绘制第一个文本,文本左上角坐标为(10, 10)
    coord += 20  # 进行多行绘制,y方向间隔20个像素
image.save('self_mutilline1.png')

draw multiline text

image = Image.open(img)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("./Gidole-Regular.ttf", size=20)
text = "Dog\nCat\nDog and Cat"
draw.text((10, 25), text, font=font)
image.save('self_mutilline2.png')

draw multiline text
6, Set text anchor alignment on the picture **

# anchor参数设置文本锚对齐
# 锚是用两个字符的字符串指定的。第一个字符是水平对齐,第二个字符是垂直对齐。
# 水平:lmrs
# 垂直:atmsbd
anchors = ['la', 'lt', 'lm', 'ls', 'lb', 'ld']
for anc in anchors:
	image = Image.new("RGB", (200, 200), "white")
	font = ImageFont.truetype(r'./Gidole-Regular.ttf', size=20)
	draw = ImageDraw.Draw(image)
	draw.line(((0, 100), (200, 100)), "gray")
	draw.line(((100, 0), (100, 200)), "gray")
	draw.text((100, 100), "Quick", fill="black", anchor=anc, font=font)
	image.save('anchor_{}.png'.format(anc))

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

6, control the text left center right

image = Image.open(img)
draw = ImageDraw.Draw(image)
alignments = ["left", "center", "right"]
y = 10
font = ImageFont.truetype(r'./Gidole-Regular.ttf', size=20)
text = "Hello from\nPillow"
for alignment in alignments:
    draw.text((10, y), text, font=font,align=alignment, fill="black")
    y += 40
image.save('align.png')

insert image description here

(2) Analysis of basic parameters (Pillow==9.3.0 version)

'''
# 基本参数解释
    # draw.text((10, 10), text, fill='red')
    # (10, 10):文本绘制的起始坐标,即文字的左上角
    # text:需要绘制的文本,字符串形式
    # fill:绘制颜色
    # font:表示一个ImageFont实例,可导入自定义的字体文件: font = ImageFont.truetype('arial.ttf', 20)
    # anchor:文本锚对齐。确定锚点与文本的相对位置。默认对齐方式是左上角。la(left-ascender) 水平文本和lt(left-top) 垂直文本。只有 OpenType/TrueType 字体支持此参数。其他字体可能会忽略该参数并使用默认(左上)对齐方式。
    # spacing:若文本传递到multiline_textsize(),则表示行之间的像素数,默认为4
    # align:若文本传递到multiline_textsize(), "left", "center" 或 "right"。确定线的相对对齐。使用anchor参数指定指向xy的对齐方式。
    # direction:文本绘制的方向,"rtl"表示从右到左,"ltr"表示从左到右,"ttb"表示从上到下。
    # features:在文本布局期间使用的 OpenType 字体功能列表。需要 libraqm。
    # language:文本的语言。不同的语言可能使用不同的字形形状或连字。此参数告诉字体文本使用的是哪种语言,并根据需要应用正确的替换(如果可用)。它应该是 BCP 47 语言代码。需要 libraqm。
    # stroke_widt:文字笔划的宽度
    # stroke_fill:文字描边的颜色。如果你不设置它,它默认为fill参数的值。
    # embedded_color:是否使用字体嵌入颜色字形(COLR 或 CBDT)
'''

Guess you like

Origin blog.csdn.net/m0_47026232/article/details/129172763