From ae7958fa8d5aa3cf852609dfa6e830c639417bc0 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 27 Nov 2023 23:03:10 +1100 Subject: [PATCH] Handle pathlib.Path in FreeTypeFont --- Tests/test_imagefont.py | 6 ++++-- src/PIL/ImageFont.py | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index db0df047f..47ba1756e 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -4,6 +4,7 @@ import re import shutil import sys from io import BytesIO +from pathlib import Path import pytest from packaging.version import parse as parse_version @@ -76,8 +77,9 @@ def _render(font, layout_engine): return img -def test_font_with_name(layout_engine): - _render(FONT_PATH, layout_engine) +@pytest.mark.parametrize("font", (FONT_PATH, Path(FONT_PATH))) +def test_font_with_name(layout_engine, font): + _render(font, layout_engine) def test_font_with_filelike(layout_engine): diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index c29562135..c3837545b 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -31,6 +31,7 @@ import sys import warnings from enum import IntEnum from io import BytesIO +from pathlib import Path from . import Image from ._util import is_directory, is_path @@ -213,6 +214,8 @@ class FreeTypeFont: ) if is_path(font): + if isinstance(font, Path): + font = str(font) if sys.platform == "win32": font_bytes_path = font if isinstance(font, bytes) else font.encode() try: