Python PIL 和基于矢量图的图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15130670/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:26:19  来源:igfitidea点击:

PIL and vectorbased graphics

pythonsvgpython-imaging-libraryvector-graphicseps

提问by floqqi

I run into several problems when I try to open EPS- or SVG-Images with PIL.

当我尝试使用 PIL 打开 EPS 或 SVG 图像时遇到了几个问题。

Opening EPS

开EPS

from PIL import Image
test = Image.open('test.eps')

ends in:

结束于:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\Lib\site-packages\PIL\Image.py", line 1965, in open
    return factory(fp, filename)
  File "C:\Python27\Lib\site-packages\PIL\ImageFile.py", line 91, in __init__
    self._open()
  File "C:\Python27\Lib\site-packages\PIL\EpsImagePlugin.py", line 206, in _open
    raise IOError, "bad EPS header"
  IOError: bad EPS header

Also opening SVG ends in IOError: cannot identify image file.

同时打开 SVG 以IOError: cannot identify image file.

The problem is I have to support both formats in my application. Converting to other formats is no alternative. I'm on Windows 7, Python 2.7.2 and PIL 1.1.7.

问题是我必须在我的应用程序中支持这两种格式。转换为其他格式别无选择。我使用的是 Windows 7、Python 2.7.2 和 PIL 1.1.7。

I uploaded both images: EPSand SVG.

我上传了两个图像:EPSSVG

回答by askewchan

PILdoesn't support svg, but you could try svglibwhich, unfortunately, does notseem to be under active development:

PIL不支持svg,但你可以试试svglib哪个,联合国幸运的是,确实 不是似乎正在积极开发中:

回答by jsbueno

There are alternatives to PIL, but alternatives to PIL are not what you want - There is no library I know of that would transparently open a vector based drawing and treat it just as any other image, short of opening a web browser and grabbing its render.

有 PIL 的替代品,但 PIL 的替代品不是你想要的 - 我知道没有任何库可以透明地打开基于矢量的绘图并将其视为任何其他图像,而无需打开 Web 浏览器并获取其渲染.

For dealing with SVG, there is a recipe using Cairo - which also can handle a lot of other formats, if a bit more difficult to deal with than the PIL API - I think Cairo can also handle EPS - so, you can probably develop your app with pycairo - or pycairo + PIL in the worst case.

对于处理 SVG,有一个使用 Cairo 的方法 - 它也可以处理很多其他格式,如果比 PIL API 更难处理 - 我认为 Cairo 也可以处理 EPS - 所以,你可以开发你的带有 pycairo 的应用程序 - 或者在最坏的情况下使用 pycairo + PIL。

The recipe for rendering SVG's is in the answer to: Convert SVG to PNG in Python

渲染 SVG 的秘诀在答案中:Convert SVG to PNG in Python

(note that you don't have to "convert the file to PNG" - the recibe shows how you render to a cairo surface, which can be displayed, saved to a file, and so on)

(请注意,您不必“将文件转换为 PNG”——recibe 显示了您如何渲染到开罗表面,可以显示、保存到文件等)

回答by Guibod

Yet, Pillow supports EMF which is still vector graphics. I stumbled on this thread as I was searching for convenient mean to convert SVG?to EMF anyway. Your best bet is to use Inkscape to create EMF from SVG:

然而,Pillow 支持 EMF,它仍然是矢量图形。我偶然发现了这个线程,因为我正在寻找将 SVG 转换为 EMF 的便捷方法。最好的办法是使用 Inkscape 从 SVG 创建 EMF:

inkscape --file image.svg --export-emf image.emf

inkscape --file image.svg --export-emf image.emf

回答by Alberto Vassena

As of today, that is July 2017, reading and converting SVG files can be easily accomplished by importing cairosvgthat provides the svg2pngfunction.

截至今天,也就是2017年7月,阅读和转换SVG文件可以很容易地通过导入完成cairosvg提供的svg2png功能。

Furthermore the svglibdevelopment is on again, thus by importing svglib and reportlab, the conversion from svg to png should be easy as well. a matter of 2 calls.

此外svglib 的开发又开始了,因此通过导入 svglib 和reportlab,从 svg 到 png 的转换也应该很容易。2个电话的问题。