3

Код:

from PIL import Image, ImageColor, ImageDraw, ImageFont

spam1=Image.new("RGB", (1500, 1000), "#000096")
spam2=ImageFont.load("arial.pil")
spam3=ImageDraw.Draw(spam1)
spam3.text((1450, 950), "IAmGM", font=spam2)
spam1.save("hello.png")

Результат:

C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Admin/Desktop/Gosha/experiment.py
Traceback (most recent call last):
  File "C:/Users/Admin/Desktop/Gosha/experiment.py", line 4, in <module>
    spam2=ImageFont.load("arial.pil")
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\ImageFont.py", line 229, in load
    f._load_pilfont(filename)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\ImageFont.py", line 69, in _load_pilfont
    with open(filename, "rb") as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'arial.pil'

И что делать?

1
  • Указать правильный путь к файлу. 24 фев 2018 в 7:53

1 ответ 1

3

Скорее всего у вас отсутствует файл с растровым шрифтом "arial.pil" или расположен в другой директории (в этом случае укажите полный путь к файлу).

PIL начиная с версии 1.1.4 поддерживает шрифты TrueType и OpenType типов. Под Windows если указанный файл шрифта найти не удалось, ImageFont.truetype() ищет указанный файл также в директории %SystemRoot%/fonts (например: C:\Windows\fonts) :

spam2=ImageFont.truetype("arial.ttf")

из Docstring:

In [7]: ImageFont.truetype?
Signature: ImageFont.truetype(font=None, size=10, index=0, encoding='', layout_engine=None)
Docstring:
Load a TrueType or OpenType font from a file or file-like object,
and create a font object.
This function loads a font object from the given file or file-like
object, and creates a font object for a font of the given size.

This function requires the _imagingft service.

:param font: A filename or file-like object containing a TrueType font.
                 Under Windows, if the file is not found in this filename,
                 the loader also looks in Windows :file:`fonts/` directory.
...

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.