nak3c
nak3c

Reputation: 449

svg image cant be opened in python

My question is how to create an svg image and save as png in the same script. Currently, I have a python script that creates an svg image; then uses a system command to convert to png. The os.system function throws the following error from within the script:

** (inkscape:1828): WARNING **: Error:  Could not open file '/mnt/d/Desktop/best_openings/testImage.svg' with VFS

Here is a script that reproduces the same error:

from IPython.display import SVG
import os
mySVG = '<svg  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect x="10" y="10" height="100" width="100" style="stroke:#ff0000; fill: #0000ff"/></svg>'
f1=open("testImage.svg", 'w+')
print >>f1,mySVG
f1.close
os.popen("inkscape -z -e testImage.png -w 1024 -h 1024 testImage.svg")

I have tried various different system calls that throw the same error:

ls | grep ".svg" | xargs -I file inkscape file -e file.png
inkscape image.svg --export-png=image.png

Upvotes: 0

Views: 9001

Answers (2)

Robert Longson
Robert Longson

Reputation: 124249

You need brackets after close to indicate it's a function call.

from IPython.display import SVG
import os
mySVG = '<svg  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect x="10" y="10" height="100" width="100" style="stroke:#ff0000; fill: #0000ff"/></svg>'
f1=open("testImage.svg", 'w+')
print >>f1,mySVG
f1.close()
os.popen("inkscape -z -e testImage.png -w 1024 -h 1024 testImage.svg")

Upvotes: 1

nak3c
nak3c

Reputation: 449

Oddly enough the following code does not throw an error:

from IPython.display import SVG
import os
mySVG = '<svg  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect x="10" y="10" height="100" width="100" style="stroke:#ff0000; fill: #0000ff"/></svg>'
f1=open("testImage.svg", 'w+')
print >>f1,mySVG
f1.close
f1=open("testImage2.svg", 'w+')
print >>f1,mySVG
f1.close
os.popen("inkscape -z -e testImage.png -w 1024 -h 1024 testImage.svg")

but there must be a better solution

Upvotes: 1

Related Questions