有没有好的Python库可以生成和渲染图像格式的文本?
我正在开发一个类似 CMS 的应用程序,用户应该能够按照他想要的方式创建菜单。理想情况下,每个菜单对象不是带有背景的文本,而是文本的图像。我用以下两种方式之一设想这一点:
a)通过在每个字母和一个给定单词的脚本中渲染一个/多个图像文件中的字体,可以给我串在一起的图像序列或单个图像文件与所有字母的组合。我知道这可以手动完成,但我觉得可能有一个库可以帮助解决这个问题。
b)使用某种图像库,能够使用多种混合效果(例如渐变颜色、阴影、发光等)渲染文本。虽然我认为这个有点难,但也许有一些东西可以满足这种需求。
关于任何执行此操作或类似操作的库有什么建议吗?
提前致谢!
布鲁诺
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我们正在使用 Imagemagick。
这将呈现一个带有文本的简单按钮:
将其包装到 Python 模块中非常简单。
We are using Imagemagick.
This will render a simple button with text:
Wrapping this into a Python module is straight forward.
虽然现在我决定使用网络字体和 CSS,但几年前我遇到了这个问题,并整理了一个小项目,根据传递的 URL 参数在文件的模板图像中生成文本。
该项目仍然在这里公开:https://bitbucket.org/jsbueno/dynabutton - 它是由作为 CGI 脚本工作,但可以轻松适应与更高效的服务器一起工作(如果您将其放在网上,我还建议进行一些安全调整)。您还可以使用它通过服务器端脚本生成所有图像,然后将生成的图像文件放在网上。
(它确实在下面使用了 PIL)
啊,是的,它可以使用适当的参数进行阴影和发光,可以使用任何服务器安装的字体,并且将使用图像模板来提供背景,因此您可以手动应用任何效果。 (不过,所包含的模板相当业余)
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)
查看 pycairo,cairo 渲染包的绑定。它可以渲染文本和图形。
Check out pycairo, bindings for the cairo rendering package. It can render text as well as graphics.
嗯,借助现代 CSS 技术,客户端字体不匹配的问题现在已经不是什么问题了。对文本转图像工具的需求仍然存在。
PIL 通常被作为这个问题的答案,但就我个人而言,我会给出一个很好的答案,也仔细看看 pythonmagick 。选择最适合您的一种。
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.
实际上 pygtk 也有一个 pango 渲染器。
Actually the pygtk also has a pango renderer, as well.