Python - Шрифт отображает всю площадь - PullRequest
0 голосов
/ 28 января 2020

У меня есть файл, который преобразует текст в изображение.

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

from random import seed
from random import randint

'''Returns the text size in terms of width and height.'''
def getSize(txt, font):
    testImg = Image.new('RGB', (1, 1))
    testDraw = ImageDraw.Draw(testImg)
    return testDraw.textsize(txt, font)

text = "As fast as thou shalt wane so fast thou grow'st,
In one of thine, from that which thou departest,
And that fresh blood which youngly thou bestow'st,
Thou mayst call thine, when thou from youth convertest,
Herein lives wisdom, beauty, and increase,
Without this folly, age, and cold decay,
If all were minded so, the times should cease,
And threescore year would make the world away:
Let those whom nature hath not made for store,
Harsh, featureless, and rude, barrenly perish:
Look whom she best endowed, she gave thee more;
Which bounteous gift thou shouldst in bounty cherish:
She carved thee for her seal, and meant thereby,
Thou shouldst print more, not let that copy die."

fontname = "MSGEOTX1.TTF" # Times New Roman Special G1 Font
fontsize = randint(10,50)  
colorText = "black"
colorOutline = "white"
colorBackground = "white"


font = ImageFont.truetype(fontname, fontsize)     
width, height = getSize(text, font)    
img = Image.new('RGB', (width+4, height+4), colorBackground)
d = ImageDraw.Draw(img)
d.text((0, 0), text, fill=colorText, font=font)
d.rectangle((0, 0, width+3, height+3), outline=colorOutline)

img.save( "image.png")

Но когда изображение отображается, оно показывает все квадратные прямоугольники. Почему это так?

enter image description here

1 Ответ

3 голосов
/ 28 января 2020

Это может быть связано с тем, что шрифт «обрезан», в основном шрифт не содержит этих символов. Поскольку шрифт не может отображать то, на чем он не отображается, на нем могут быть проблемы с шрифтом Попробуйте изменить другой шрифт или попробуйте другой способ его отображения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...