使用 Ruby 或 Python 创建静态图形文件(png、gif、jpg)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create static graphics files (png, gif, jpg) using Ruby or Python
提问by Charlie Martin
I'd like to create a graphic image on the fly based on user input, and then present that image as a PNG file (or jpg or gif if necessary, but PNG is preferred).
我想根据用户输入动态创建图形图像,然后将该图像显示为 PNG 文件(或 jpg 或 gif 如有必要,但首选 PNG)。
This is actually for an astrology application; what I'd like to do is generate the chart in PNG for display.
这实际上是用于占星术的应用程序;我想做的是生成 PNG 格式的图表以供显示。
Python or Ruby is fine; in fact, the library available may determine the language I use.
Python 或 Ruby 都可以;事实上,可用的库可能决定了我使用的语言。
Update
更新
Here's an example image:
这是一个示例图像:
采纳答案by Gishu
I found Gruffeasy to use when I was in your shoes. Shameless blog plug.
回答by Roberto Bonvallet
Maybe a vectorial format is better suited for your needs, but is hard to tell without having a concrete example of what you'd like to get.
也许矢量格式更适合您的需求,但如果没有您想要获得的具体示例,则很难说。
For example, if the images are all alike, you could create a SVG base image with Inkscape, then edit it programmaticaly from Python or Ruby (either by editing the text or using a XML library) and finally export it to PNG.
例如,如果图像都相似,您可以使用 Inkscape 创建一个 SVG 基础图像,然后从 Python 或 Ruby 以编程方式对其进行编辑(通过编辑文本或使用 XML 库),最后将其导出为 PNG。
Update:After seeing the example image, I think SVG would be the most convenient choice. A SVG image is an XML file that basically says "draw a circle from here to here, write the string '13o52' there", etc. You could draw a unique base chart in Inkscape and have your program just adding the lines and symbols for each case. Finally you export to PNG.
更新:看到示例图像后,我认为 SVG 将是最方便的选择。SVG 图像是一个 XML 文件,基本上说“从这里到这里画一个圆圈,在那里写字符串 '13o52'”等等。你可以在 Inkscape 中绘制一个独特的基本图表,让你的程序只添加线条和符号每个案例。最后导出为 PNG。
The advantages are: easier for you to draw, the image is fully scalable, you can change the styling just by editing a property ("make all lines wider", "change all text to Arial", "paint the background blue"), you can export to any format without losing quality, and I think it's more mantainable.
优点是:更容易绘制,图像完全可缩放,只需编辑属性即可更改样式(“使所有线条更宽”,“将所有文本更改为 Arial”,“将背景绘制为蓝色”),您可以导出为任何格式而不会降低质量,而且我认为它更易于维护。
回答by kquinn
In Python, you'd typically use PIL, the Python Image Library. I've never used PIL for anything beyond the simplest tasks, so I can't say how well it performs in practice.
在 Python 中,您通常会使用 PIL,即 Python 图像库。我从来没有将 PIL 用于最简单的任务之外的任何事情,所以我不能说它在实践中的表现如何。
I'd start digging into PIL with a look at its documentation, particularly the documentation for the draw module.
回答by Van Gale
For Python I the most common choice for image formats is PIL and then Pycairofor vector formats. The two can work together, for exadmple in this cookbook entry to use PIL images for Pycairo surfaces.
对于 Python,图像格式最常见的选择是 PIL,然后是矢量格式的Pycairo。这两者可以一起工作,例如在这个食谱条目中使用 PIL 图像来处理 Pycairo 表面。
回答by greyfade
回答by bayda
when I was doing python chalange (http://www.pythonchallenge.com/) I've used Python Image Library (http://www.pythonware.com/library/pil/)
当我在做 python chalange ( http://www.pythonchallenge.com/) 我用过 Python Image Library ( http://www.pythonware.com/library/pil/)