Is there any good python library for generating and rendering text in image format?
I'm developing a CMS like application where the user should be able to create the menu the way he wants. Ideally, each menu object wouldn't be a text with a background but rather an image of the text. I envision this in either of these 2 ways:
a) By rendering a font in a/several image file/s with every letter and a script that given a word would give me either the sequence of images to string together or a single image file with the combination of all letters. I understand this could be done manually, but I feel there's probably a library to help with this.
b) With some kind of imaging library that would be able to render text with several blending effects such as gradient开发者_开发知识库 color, shadows, glow, etc. While I believe this one's a bit harder, maybe there's something that suits this need.
Any tips on any library that does this or anything similar?
Thanks in advance!
Bruno
We are using Imagemagick.
http://www.imagemagick.org/Usage/text/#attributes
This will render a simple button with text:
convert -background white -fill dodgerblue -font Candice -strokewidth 2 -stroke blue -undercolor lightblue -size 165x70 -gravity center label:Anthony label_color.gif
Wrapping this into a Python module is straight forward.
Although nowadays I'd settle to go with web fonts and CSS,a couple of years ago I faced this problem, and put together a small project that would generate text-within a templated image on the file, according to passed URL parameters.
The project is still publicized here: https://bitbucket.org/jsbueno/dynabutton -- it is made to work as a CGI script, but could be easily adapted to work with a more eficient server (I'd recomend some security tunning as well, if you are putting it online). You can also use it to generate all your images with a server side script, and just put the resulting image files online.
(it does use PIL underneath)
Ah yes, it can do shadow, and glow with proper parameters, can use any server-installed font, and will use an image template for providing the background, so you can apply any effect manually. (the included temlates, though, are quite amateurish)
Check out pycairo, bindings for the cairo rendering package. It can render text as well as graphics.
Well, with modern CSS techniques, the issue of nonmatching client-side fonts is less of a problem these days. Still there's demand for text-to-image tools.
PIL is often given as the answer to this question, but personally, I would give a good, hard look at pythonmagick as well. Pick the one that works best for you.
Actually the pygtk also has a pango renderer, as well.
精彩评论