[Image-SIG] ImageFont Unicode Decode error

Fredrik Lundh fredrik at pythonware.com
Wed Jan 2 10:49:11 CET 2008


Donn wrote:

>  Trying to open a ttf file with a non-standard character in it (u'\xe5') 
> causes an error, no matter what I pass to encoding:

>     self.font = _imagingft.getfont(file, size, index, encoding)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 
> 66: ordinal not in range(128)
> 
> Is there something I'm doing wrong, or is this a bug?

a glitch, at least.  the _imagingft driver appears to use Python's 
default encoding, rather than the filename encoding.

should be relatively easy to fix, I think, but in the meantime, you'll 
have to work around this by making a temporary copy of the file with a 
made-up name if this happens:

     try:
         font = ImageFont.truetype(filename, ...)
         altname = None
     except UnicodeEncodeError:
         # create a suitable alternative name (e.g. using the tempfile
         # module)
         altname = ...
         shutil.copy(filename, altname) # could use linking on Unix
         font = ImageFont.truetype(altname, ...)

     ... use the font object ...

     if altname:
         os.remove(altname)

</F>



More information about the Image-SIG mailing list