2

What is the most speedy way and accurate to render non-anti-aliased fonts (e.g. ttf fonts) using Python to an internal image (e.g. to a PIL.Image, i.e. I don't need to display it)? I say accurate because I tried it with pygame a while back and the rendered fonts at the size I gave it didn't match what windows rendered in Word or Paint.

martineau
  • 119,623
  • 25
  • 170
  • 301
Claudiu
  • 224,032
  • 165
  • 485
  • 680

2 Answers2

8

Python Imaging Library (PIL) can render text to an image--I'm not aware of it being inaccurate, but I haven't fully tested it yet...

Example from a pre-existing question:

from PIL import Image
from PIL import ImageFont, ImageDraw

image = Image.new("RGBA", (288,432), (255,255,255))
usr_font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", 25)
d_usr = ImageDraw.Draw(image)
d_usr.fontmode = "1" # this apparently sets (anti)aliasing.  See link below.
d_usr.text((105,280), "MYTEXT",(0,0,0), font=usr_font)

See also:

http://www.pythonware.com/products/pil/

http://mail.python.org/pipermail/image-sig/2005-August/003497.html

Python Imaging Library - Text rendering

Community
  • 1
  • 1
elliot42
  • 3,694
  • 3
  • 26
  • 27
  • ah this inaccuracy is just what i mean. but i've posted another question about it [here](http://stackoverflow.com/questions/5748973/why-is-my-truetype-font-of-size-11-rendering-different-than-windows). – Claudiu Apr 21 '11 at 19:41
  • ah this actually renders it as anti-aliased, and i want it to not be. any tip on how to do that? – Claudiu Apr 21 '11 at 20:09
  • 1
    @Claudiu edited answer based on this page: http://mail.python.org/pipermail/image-sig/2005-August/003497.html – elliot42 Apr 21 '11 at 21:26
  • nice! that does indeed render it un-anti-aliased. it's still different from notepad, so =( for me, but from the other question i asked theres no way to get PIL to do that – Claudiu Apr 21 '11 at 23:14
1

Ahh this is how it's done... should render the same as windows at is uses its algorithm.

Community
  • 1
  • 1
Claudiu
  • 224,032
  • 165
  • 485
  • 680