@@ -98,11 +98,13 @@ class ImageFont:
9898 def _load_pilfont (self , filename : str ) -> None :
9999 with open (filename , "rb" ) as fp :
100100 image : ImageFile .ImageFile | None = None
101+ root = os .path .splitext (filename )[0 ]
102+
101103 for ext in (".png" , ".gif" , ".pbm" ):
102104 if image :
103105 image .close ()
104106 try :
105- fullname = os . path . splitext ( filename )[ 0 ] + ext
107+ fullname = root + ext
106108 image = Image .open (fullname )
107109 except Exception :
108110 pass
@@ -112,7 +114,8 @@ def _load_pilfont(self, filename: str) -> None:
112114 else :
113115 if image :
114116 image .close ()
115- msg = "cannot find glyph data file"
117+
118+ msg = f"cannot find glyph data file { root } .{{gif|pbm|png}}"
116119 raise OSError (msg )
117120
118121 self .file = fullname
@@ -224,7 +227,7 @@ def __init__(
224227 raise core .ex
225228
226229 if size <= 0 :
227- msg = "font size must be greater than 0"
230+ msg = f "font size must be greater than 0, not { size } "
228231 raise ValueError (msg )
229232
230233 self .path = font
@@ -783,8 +786,9 @@ def getlength(self, text: str | bytes, *args: Any, **kwargs: Any) -> float:
783786
784787def load (filename : str ) -> ImageFont :
785788 """
786- Load a font file. This function loads a font object from the given
787- bitmap font file, and returns the corresponding font object.
789+ Load a font file. This function loads a font object from the given
790+ bitmap font file, and returns the corresponding font object. For loading TrueType
791+ or OpenType fonts instead, see :py:func:`~PIL.ImageFont.truetype`.
788792
789793 :param filename: Name of font file.
790794 :return: A font object.
@@ -804,9 +808,10 @@ def truetype(
804808) -> FreeTypeFont :
805809 """
806810 Load a TrueType or OpenType font from a file or file-like object,
807- and create a font object.
808- This function loads a font object from the given file or file-like
809- object, and creates a font object for a font of the given size.
811+ and create a font object. This function loads a font object from the given
812+ file or file-like object, and creates a font object for a font of the given
813+ size. For loading bitmap fonts instead, see :py:func:`~PIL.ImageFont.load`
814+ and :py:func:`~PIL.ImageFont.load_path`.
810815
811816 Pillow uses FreeType to open font files. On Windows, be aware that FreeType
812817 will keep the file open as long as the FreeTypeFont object exists. Windows
@@ -942,7 +947,10 @@ def load_path(filename: str | bytes) -> ImageFont:
942947 return load (os .path .join (directory , filename ))
943948 except OSError :
944949 pass
945- msg = "cannot find font file"
950+ msg = f'cannot find font file "{ filename } " in sys.path'
951+ if os .path .exists (filename ):
952+ msg += f', did you mean ImageFont.load("{ filename } ") instead?'
953+
946954 raise OSError (msg )
947955
948956
0 commit comments