jack
jack

Reputation: 17881

Methods to create text preview from a font

I'm currently using ImageMagick's convert command to create text preview (.png) from .ttf font file. Overall, it's better in auto text positioning despite it failed to read some valid .ttf file sometimes. The speed is not great but acceptable.

PIL's ImageFont looks like is not good at text aligning, often prints bottom-left corner of first character outside canvas.

Does anyone know any better choices beside the above two? I wonder what tech is needed to power the text preview part on sites like myfonts.com with so heavy traffic.

EDIT

Example of PIL Font failed to draw fonts correctly. Font used in the example is Hanford script

import Image, ImageDraw, ImageFont
font = ImageFont.truetype('HANFORD_.TTF', 122)
text_width, text_height = font.getsize('Hanford script')
print text_width, text_height
>>> 833 47
img = Image.new('RGBA', (text_width, text_height))
draw = ImageDraw.Draw(img)
draw.text((0, 0), 'Hanford script', font=font, fill=(0, 0, 0))
img.save('output.png')

The output image only contains upper half of "Hanford script".

I tried with imagemagick's convert command:

convert -font "HANFORD_.TTF" -background transparent -gravity center -size 830x80 label:'Hanford script' output.png

The output image was same as what I got by PIL.

It's not the only font that PIL or imagemagick cannot get correct text size. With some other fonts, like Ginga, they just amplify text height which result in upper half of output images are blank. Anyone knows the reason?

Upvotes: 1

Views: 20553

Answers (3)

lafihay530
lafihay530

Reputation: 1

I also had the same problem. A few days ago, I discovered this library. I tried it and it seemed really useful. I share the link as I also had the same problem.

https://github.com/MatteoGuadrini/fontpreview

Example of font preview:

from fontpreview import FontPreview

fp = FontPreview('/tmp/noto.ttf')
fp.save('/tmp/fp.png')

Upvotes: 0

James
James

Reputation: 25513

I imagine that such sites are driven by a little custom code using something like Pango, or just bare-bones Freetype at the back end to render to image files.

The images may well never even hit the disc, although I'm sure things are cached where appropriate.

Upvotes: 1

Peter Rowell
Peter Rowell

Reputation: 17713

This is sort of off-the-wall, but have you looked at sIFR (Scalable Inman Flash Replacement)? It is used to allow web designers to insert short text chunks (headings and such) with text rendered in any font they choose to use, even though the user does not have it installed locally.

If you were to create a simple web page using sIFR and the served it locally to yourself it might suit your purposes.

Like I said, it's kind of a screwy solution, but ... :-)

Upvotes: 0

Related Questions