PIL question. having exactly same font on multiple platforms


G

Gelonida N

I posted already a question, but perhaps the subject line wasn't clear.


Subject line was "Text to image with same results on any platform"

From within a django application
I'd like create a small image file (e.g. .png)
which just contains some text.

I wondered what library would be appropriate and would yield the same
result independent of the OS (assuming the versions of the python
libraries are the same)
Images should be pixel identical independent on the platform on which
the image is created.

I made some attempts with PIL (Image / ImageFont / ImageDraw),
but have difficulties getting the same font under Linux and windows.


# ------------------------code starts here
import Image
import ImageFont
import ImageDraw

# Here I don't know how to get the same font file
# for all platforms
# Perhaps there is a tiny trick that I overlooked.
font_file = get_font_file_name()
# Is truetype a good choice for portable fonts?
font = ImageFont.truetype(font_file, 20)

img = Image.new("RGB", (800, 600), "#FFFFFF")
draw = ImageDraw.Draw(image)

draw.text( (100, 100), font=font, fill="#00FF00")
# ---------------------------- end of code


I could also install any other library (matplotlib??)
if it were capable of creating consistent images
independent on the platform.

Thanks in advance for any suggestions.
 
Ad

Advertisements

S

Steven D'Aprano

Gelonida said:
I wondered what library would be appropriate and would yield the same
result independent of the OS (assuming the versions of the python
libraries are the same)
Images should be pixel identical independent on the platform on which
the image is created.

Short answer: you can't.

Long answer: you might, if you can find identical fonts (not just fonts with
the same name, but genuinely identical) for each platform, and find some
way of telling each platform's font rendering software to use identical
algorithms (e.g. kerning, hinting, scaling, sub-pixel rendering), and
adjust for any other differences between platforms (e.g. font sizes on
Windows tend to be larger than on Mac or Linux).

And when I say "you might", I mean it is theoretically possibly, but
practically speaking, you can't.

Even longer answer: you can, if you ignore the platform font renderer, and
use your own custom renderer; this is what (apparently) a number of
Linux/Unix applications like Abiword and xpdf do (with varying levels of
success). You still need to find identical fonts for each platform.
Possibly the simplest way to do this is to stick to old-fashioned bitmapped
fonts in fixed sizes, instead of Postscript or TrueType fonts -- assuming
you don't mind your application's output looking like it was produced on a
Macintosh in 1984.

More information here:

https://freddie.witherden.org/pages/font-rasterisation/
http://www.joelonsoftware.com/items/2007/06/12.html
http://www.codinghorror.com/blog/2007/06/whats-wrong-with-apples-font-rendering.html
http://jujusoft.com/?page_id=25
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top