@@ -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+ filename_body = 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 = filename_body + ext
106108 image = Image .open (fullname )
107109 except Exception :
108110 pass
@@ -112,7 +114,9 @@ def _load_pilfont(self, filename: str) -> None:
112114 else :
113115 if image :
114116 image .close ()
115- msg = "cannot find glyph data file"
117+
118+ pre = filename_body
119+ msg = f"cannot find glyph data file { pre } .{{png|gif|pbm}}"
116120 raise OSError (msg )
117121
118122 self .file = fullname
@@ -224,7 +228,7 @@ def __init__(
224228 raise core .ex
225229
226230 if size <= 0 :
227- msg = "font size must be greater than 0"
231+ msg = f "font size must be greater than 0, not { size } "
228232 raise ValueError (msg )
229233
230234 self .path = font
@@ -774,6 +778,8 @@ def load(filename: str) -> ImageFont:
774778 :param filename: Name of font file.
775779 :return: A font object.
776780 :exception OSError: If the file could not be read.
781+
782+ .. seealso:: :py:func:`PIL.ImageFont.truetype`
777783 """
778784 f = ImageFont ()
779785 f ._load_pilfont (filename )
@@ -850,6 +856,8 @@ def truetype(
850856 :return: A font object.
851857 :exception OSError: If the file could not be read.
852858 :exception ValueError: If the font size is not greater than zero.
859+
860+ .. seealso:: :py:func:`PIL.ImageFont.load`
853861 """
854862
855863 def freetype (font : StrOrBytesPath | BinaryIO | None ) -> FreeTypeFont :
@@ -927,7 +935,10 @@ def load_path(filename: str | bytes) -> ImageFont:
927935 return load (os .path .join (directory , filename ))
928936 except OSError :
929937 pass
930- msg = "cannot find font file"
938+ msg = f"cannot find font file '{ filename } ' in `sys.path`"
939+ if os .path .exists (filename ):
940+ msg += f" did you mean `ImageFont.load({ filename } )` instead?"
941+
931942 raise OSError (msg )
932943
933944
0 commit comments