5
votes

What fonts are available to use with PIL on App Engine?

I know I can upload a font as part of the application but what I basically want is a system provided sans-serif that will work with unicode characters that is a good looking Helvetica (or alternative). Google servers may already have some fonts installed and this is what I would like some insight into.

I would like to find a font to use that is unicode capable so Japanese and other characters will render properly using PIL.

I have a web page that renders unicode characters properly using font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;

When I use PIL to render the same unicode characters they do not render properly. The font I uploaded as part of the app does not have the necessary unicode characters in the font.

Here are the relevant python lines of code from what I am using now.

import Image, ImageDraw, ImageFont  

draw = ImageDraw.Draw(my_output)  
font = ImageFont.truetype("FontIAmUsing.otf", font_size, encoding="utf-8")  
draw.text((5, 51), 'abc', fill='#2A2A2A', font=font)
1
How are you using the font? Do you have some code snippets? (I'm not too familiar with PIL and trying to do somethings to look around. e.g. import os, PIL; print os.listdir(os.path.dirname(PIL.__file__)) from shell-27.appspot.com) - bossylobster
Where does the file "FontIAmUsing.otf" typically live? IIUC, it's supposed to be in the directory you're calling the code in. Could you not just include the font with your application and deploy with the file? This article seems to cover where fonts are kept and how you might include them locally. - bossylobster
Right. I've read that article and I do upload my font. I am hoping that there is a system font that will "just work" with unicode characters like sans-serif does for web pages that use unicode characters. On a Mac, I think sans-serif falls back to Helvetica. I don't have a Helvetica font with unicode characters to upload as part of the app. I'm still hoping that Google might have some system installed fonts (Helvetica?) that I could make use of. - philipfc
AFAICT, there are no system fonts uploaded, even the few distributed with PIL by default. Still trying to find out. - bossylobster

1 Answers

4
votes

Considering that PIL only ships with a default bitmap font (ugly and probably not containing a very wide range of unicode characters) and since you have to provide the full path to your font file AND you cannot get outside of your sandboxed environment in GAE, you are better just uploading your fonts along with your application. If you don't specify a path, it will look at the root of your web application. I am using the GoogleAppEngineLauncher for OSX and when deploying it uploads all the files found (including the .ttf I am using with ImageDraw).

The documentation at http://effbot.org/imagingbook/imagefont.htm states: (New in 1.1.4) Load a "better than nothing" default font. I just confirmed that the default font is available on GAE.