Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL and vectorbased graphics

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.

like image 708
floqqi Avatar asked Feb 28 '13 08:02

floqqi


People also ask

What is a vector based graphics program?

Vector graphics software allows users to design and manipulate computer images using geometric and mathematical commands, rather than clicks and strokes as used in drawing software. Vector images created using these programs can be scaled indefinitely without losing quality.

What is vectors in graphic design?

Vector graphics are computer images created using a sequence of commands or mathematical statements that place lines and shapes in a two-dimensional or three-dimensional space. In vector graphics, a graphic artist's work, or file, is created and saved as a sequence of vector statements.

What are vector graphics used for and why?

Vector graphics are made up by points (or coordinates) on a screen that are connected through lines and curves called paths. Vector graphics are widely used for creating logos, line art, 3D-like renderings, and animations, among other examples.


2 Answers

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.

like image 97
Alberto Vassena Avatar answered Nov 16 '22 01:11

Alberto Vassena


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.

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.

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

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

like image 36
jsbueno Avatar answered Nov 15 '22 23:11

jsbueno