PIL and vectorbased graphics

floqqi picture floqqi · Feb 28, 2013 · Viewed 29.8k times · Source

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

Opening 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.

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.

I uploaded both images: EPS and SVG.

Answer

Alberto Vassena picture Alberto Vassena · Jul 23, 2017

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

Furthermore the svglib development 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.