PIL and vectorbased graphics
Asked Answered
A

5

26

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.

Apparatus answered 28/2, 2013 at 8:21 Comment(2)
PIL doesn't support SVG format. EPS format is not fully supported. See pythonware.com/library/pil/handbook/index.htm.Byline
So, is there any alternative to PIL in the python world?Apparatus
L
15

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.

Lumbard answered 23/7, 2017 at 7:25 Comment(3)
To add on this answer, it is a matter of just three commands (plus imports): after from io import BytesIO, import cairosvg, from PIL import Image, just run out = BytesIO(), cairosvg.svg2png(url='path/to/svg', write_to=out), image = Image.open(out)Tillage
After a lot of search, I found svglib and reportlab to be the best options! Installed using pip, called and it just worked - lovely! Thanks!Yorgo
It might be worth noting that python has no good way of support SVG 2.0. Both cairosvg and svglib only support up to SVG 1.1.Manganate
P
9

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)

Peripteral answered 15/3, 2013 at 18:1 Comment(0)
M
1

PIL doesn't support svg, but you could try svglib

Mel answered 15/3, 2013 at 17:26 Comment(1)
Luckily, svglib development seems to be now back again. Version 0.8.1 currently in the PyPI supports Python 2.7 and 3.5.Lumbard
T
1

Yet, Pillow supports EMF (Enhanced Windows Metafile) 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:

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

Tranche answered 25/4, 2017 at 14:25 Comment(0)
D
0

While I believe the top answer is better, one is able to leverage Inkscape to render SVG to PNG files.

render_image_command = "inkscape --export-background-opacity=0 --export-width={} --export-type=png --export-filename {} {}"

render_image_command = render_image_command.format(
    image_width_in_pixels,
    path_to_png_image_to_write,
    path_to_svg_image_to_read
)
Dreamadreamer answered 8/12, 2021 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.