kast3t
@kast3t

Хочу понять, что делает метод font.getoffset?

Привет.
Приведенный ниже целый код распечатывает текст на картинке по центру изображения.

Я не могу понять эту строчку. Что делает метод getoffset? С помощью отладчика я вижу, что в offset_x - лежит число -1, в offset_y - число 18:
Строчка из кода: offset_x, offset_y = font.getoffset(text)

Целый код:

from PIL import Image, ImageDraw, ImageFont

color = (255, 244, 41)
text = 'S'

N = 500
size_image = width_image, height_image = N, N

img = Image.new('RGB', size_image, color='white')
font_path = './fonts/BebasNeue-Regular.ttf'
font = ImageFont.truetype(font_path, size=600)
draw = ImageDraw.Draw(img)
width_text, height_text = draw.textsize(text, font)

offset_x, offset_y = font.getoffset(text)
width_text += offset_x
height_text += offset_y

top_left_x = width_image / 2 - width_text / 2
top_left_y = height_image / 2 - height_text / 2
xy = top_left_x, top_left_y

draw.text(xy, text, font=font, fill=color)

img.show()
  • Вопрос задан
  • 45 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Kadabrov
Returns the offset of given text. This is the gap between the starting coordinate and the first marking. Note that this gap is included in the result of getsize().
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы