Are svg relative paths supported

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Danack
Posts: 73
Joined: 2013-10-14T10:00:25-07:00
Authentication code: 6789

Are svg relative paths supported

Post by Danack »

Hi,

Are relative paths for linking images in SVG files supported?

For example given the two input files "subdir/red.svg":

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="100" height="100" viewBox="0 0 100 100" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" width="80" height="80" fill="red"/>
<image x="30" y="30" width="40" height="40" xlink:href="green.svg" preserveAspectRatio="xMidYMid meet"/>
</svg>
and "subdir/green.svg":

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="100" height="100" viewBox="0 0 100 100" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" width="80" height="80" fill="green"/>
</svg>

Running the command `convert subdir/red.svg test.png` in the directory above these files, gives the error:
"convert: unable to open image `green.svg': No such file or directory @ error/blob.c/OpenBlob/2709."
Changing the "subdir/red.svg" file to:

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="100" height="100" viewBox="0 0 100 100" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" width="80" height="80" fill="red"/>
<image x="30" y="30" width="40" height="40" xlink:href="file:./green.svg" preserveAspectRatio="xMidYMid meet"/>
</svg>
which I believe is the correct format for relative file paths, gives the error:
convert: unable to open image `green.svg': No such file or directory @ error/blob.c/OpenBlob/2709.
convert: no data returned `file:./green.svg' @ error/url.c/ReadURLImage/265.
So I'm guessing they're not currently supported. Is that just because it's not supported or a bug?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Are svg relative paths supported

Post by fmw42 »

This command works OK for me on Mac OSX Snow Leopard with IM 6.9.0.3 Q16 from my home directory. In fact I can put the file in a folder on the desktop and add that to path and it works fine also.

Code: Select all

convert desktop/puzzle_tile_new.svg puzzle_tile_new.png
Check your directory permissions.
Danack
Posts: 73
Joined: 2013-10-14T10:00:25-07:00
Authentication code: 6789

Re: Are svg relative paths supported

Post by Danack »

I have checked my directory permissions. I can't see a problem with them.

I have put the exact code I'm using to test with below. This is on Centos 6.4

Code: Select all


read -d '' red << EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="100" height="100" viewBox="0 0 100 100" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" width="80" height="80" fill="red"/>
<image x="30" y="30" width="40" height="40" xlink:href="green.svg" preserveAspectRatio="xMidYMid meet"/>
</svg>
EOF

read -d '' green << EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="100" height="100" viewBox="0 0 100 100" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" width="80" height="80" fill="green"/>
</svg>
EOF

mkdir "svgSubDir"

echo $red > "svgSubDir/red.svg"
echo $green > "svgSubDir/green.svg"

convert svgSubDir/red.svg test.png

# output is:
# convert: unable to open image `green.svg': No such file or directory @ error/blob.c/OpenBlob/2709.

cd svgSubDir

convert red.svg testInSubdir.png

# output is:
# convert: unable to open image `green.svg': No such file or directory @ error/blob.c/OpenBlob/2709.
The command works fine when run in the sub-directory.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Are svg relative paths supported

Post by snibgo »

When reporting an SVG problem, it helps if you say which SVG renderer you are using. "-verbose" will tell you.

For me, IM v6.9.0-0 on Windows 8.1, the renderers Inkscape and RSVG work fine. MSVG doesn't, giving

Code: Select all

convert.exe: unable to open image `green.svg': No such file or directory @ error/blob.c/OpenBlob/2709.
Of the three, MSVG is usually the flakiest.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Are svg relative paths supported

Post by fmw42 »

For your green svg code:

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="100" height="100" viewBox="0 0 100 100" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" width="80" height="80" fill="green"/>
</svg>
The following both work for me on IM 6.9.0.3 Q16 Mac OSX Snow Leopard with delegates

libxml @1.8.17_0 (active) for MSVG
librsvg @2.40.2_0+viewer (active) for RSVG

Code: Select all

convert MSVG:green.svg green_msvg.png

Code: Select all

convert RSVG:green.svg green_rsvg.png
Check what delegates you have installed for reading and writing SVG files. It should be listed in

convert -version

on current versions of IM.

What version of IM and platform are you using?



Also check your delegates.xml file to see what "command" tool is being used if you do not preface the file with MSVG or RSVG or INKSCAPE. Perhaps it wants one that is not installed. Mine says


<delegate decode="svg" command=""rsvg-convert" -o "%o" "%i""/>

<delegate decode="svg:decode" stealth="True" command=""inkscape" "%s" --export-png="%s" --export-dpi="%s" --export-background="%s" --export-background-opacity="%s" > "%s" 2>&1"/>
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Are svg relative paths supported

Post by dlemstra »

snibgo wrote: For me, IM v6.9.0-0 on Windows 8.1, the renderers Inkscape and RSVG work fine. MSVG doesn't, giving

Code: Select all

convert.exe: unable to open image `green.svg': No such file or directory @ error/blob.c/OpenBlob/2709.
Of the three, MSVG is usually the flakiest.
You are correct this looks like an issue in the MSVG reader, we are investigating it.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Are svg relative paths supported

Post by fmw42 »

Must be windows issue, since it works fine for me on my Mac using

Code: Select all

convert MSVG:green.svg green_msvg.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Are svg relative paths supported

Post by snibgo »

But the problem occurs (for me) when both red.svg and green.svg are in a subdirectory of the current directory, and the command is:

Code: Select all

convert MSVG:subdir/red.svg test.png
red.svg, not green.svg.

EDIT to add: the parser should read red.svg, see

Code: Select all

xlink:href="green.svg"
and look for green.svg in the same directory as red.svg. I suspect it is looking in the current directory, instead.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Are svg relative paths supported

Post by fmw42 »

Sorry, I forgot about the subdirectories. Nevertheless, the basic convert commands work fine for me on my Mac with subdirectories.

Code: Select all

convert MSVG:desktop/test/green.svg green_msvg.png

Code: Select all

convert RSVG:desktop/test/green.svg green_rsvg.png
Does quoting the input help on Windows?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Are svg relative paths supported

Post by snibgo »

Yeah, but what about converting red.svg?

For me, quoting at the command line makes no difference. I wouldn't expect it to.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Are svg relative paths supported

Post by fmw42 »

Yes, snibgo, you are right. It does not work with MSVG

This fails with MSVG

Code: Select all

convert MSVG:desktop/test/red.svg red_msvg.png
convert: unable to open image `green.svg': No such file or directory @ error/blob.c/OpenBlob/2701.

But this works with RSVG

Code: Select all

convert RSVG:desktop/test/red.svg red_rsvg.png

I thought the red and green were the same apart from the color (from too cursory a look), but I see now that red has a link to green.

Code: Select all

xlink:href="green.svg"
I suspect you are right about MSVG in that the path indicates the current directory where the command is initiated rather than the directory in the path in the command.

Changing the href to

Code: Select all

xlink:href="desktop/test/green.svg"
allows it to work for me with MSVG. So I think that confirms that MSVG is looking to current directory from which the command is initiated.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Are svg relative paths supported

Post by dlemstra »

MSVG will only read files from the current directory. We won't add support for relative include paths, you should use rsvg/inkscape as a delegate if you want support for that.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Danack
Posts: 73
Joined: 2013-10-14T10:00:25-07:00
Authentication code: 6789

Re: Are svg relative paths supported

Post by Danack »

Thanks. I am seeing this with Inkscape, but an older version before it was fixed:
https://bugs.launchpad.net/inkscape/+bug/386069

So it's not something to do with ImageMagick.
Post Reply