Is there any good python library for generating and rendering text in image format?

烂漫一生 提交于 2019-12-11 05:12:26

问题


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


回答1:


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)




回答2:


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.




回答3:


Check out pycairo, bindings for the cairo rendering package. It can render text as well as graphics.




回答4:


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.




回答5:


Actually the pygtk also has a pango renderer, as well.



来源:https://stackoverflow.com/questions/5163416/is-there-any-good-python-library-for-generating-and-rendering-text-in-image-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!