svg loading ignores -density

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
roelandsch
Posts: 20
Joined: 2014-10-27T16:10:38-07:00
Authentication code: 6789

svg loading ignores -density

Post by roelandsch » 2017-03-09T02:08:59-07:00

Hi, I'm trying to convert a SVG file to PNG or JPG, but I'm trying to load with a different resolution than the default.

test.svg

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="100" height="100">
<circle r="29" cy="38" cx="36" style="opacity:.5" />
<circle r="29" cy="63" cx="66" style="opacity:.5" />
</svg>

Code: Select all

convert -density 180 test.svg test1.png
convert -density 45 test.svg test2.png
However the density argument has no effect. Both PNG files are 100×100. I tried this on another PC, there this works as expected. What would possibly affect whether this option works or not?

ImageMagick version on this PC is ImageMagick 6.9.7:

Code: Select all

Version: ImageMagick 6.9.7-10 Q16 x64 2017-03-04 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib
It appears to use RSVG:

Code: Select all

>convert -list format
   ...
      SVG  SVG       rw+   Scalable Vector Graphics (RSVG 2.40.15)
     SVGZ  SVG       rw+   Compressed Scalable Vector Graphics (RSVG 2.40.15)
I can check the version on the other PC later.

snibgo
Posts: 12153
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: svg loading ignores -density

Post by snibgo » 2017-03-09T02:49:17-07:00

It works as expected for me. On my system, IM delegates the rasterisation to Inkscape. I know this because "convert -verbose x.svg x.png" shows me the Inkscape command.
snibgo's IM pages: im.snibgo.com

roelandsch
Posts: 20
Joined: 2014-10-27T16:10:38-07:00
Authentication code: 6789

Re: svg loading ignores -density

Post by roelandsch » 2017-03-09T12:14:07-07:00

That's not the case here:

Code: Select all

>convert -density 300 -verbose test.svg test.png
test.svg SVG 100x100 100x100+0+0 16-bit sRGB 229B 0.016u 0:00.022
test.svg=>test.png SVG 100x100 100x100+0+0 8-bit sRGB 100c 1.68KB 0.016u 0:00.030
I can enable Inkscape in the delegates.xml file:

Code: Select all

>convert -density 300 -verbose test.svg test.png
"C:\Program Files\Inkscape\inkscape" "C:/Users/roeland/AppData/Local/Temp/magick-4920CH54btpt90KR" --export-eps="C:/User
s/roeland/AppData/Local/Temp/magick-49209hhiH5aQG0wZ" --export-dpi="300,300" --export-background="rgb(100%,100%,100%)" --export-background-opacity="1" > "C:/Users/roeland/AppData/Local/Temp/magick-4920R3u2DBN_0Uek" 2>&1
test.svg SVG 100x100 100x100+0+0 16-bit sRGB 229B 0.016u 0:01.021
test.svg=>test.png SVG 100x100 100x100+0+0 8-bit sRGB 100c 1.68KB 0.016u 0:00.009
Which, quite surprisingly, still outputs a 100×100 image.

Maybe it's system dependent — This is on a Windows 7 installation, I installed using the installer (ImageMagick-6.9.7-10-Q16-HDRI-x64-dll.exe) I downloaded yesterday.

Edit: The version on the other PC is a bit older, it's ImageMagick 6.9.3-3. Here using -density has the expected effect. This one is using RSVG as well.

snibgo
Posts: 12153
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: svg loading ignores -density

Post by snibgo » 2017-03-10T04:57:12-07:00

I used IM v6.9.5-3 and Inkscape v0.91. If your Inkscape is old, it may not respond to ...

Code: Select all

--export-dpi="300,300"
... in its command line.
snibgo's IM pages: im.snibgo.com

roelandsch
Posts: 20
Joined: 2014-10-27T16:10:38-07:00
Authentication code: 6789

Re: svg loading ignores -density

Post by roelandsch » 2017-03-10T19:40:46-07:00

Just tested that, Inkscape responds to the DPI argument. I have v0.91 as well. Given this command line the PNG file will be half the size:

Code: Select all

>inkscape.exe test.svg --export-dpi="45,45" --export-png=test.png
But ImageMagick is calling inkscape with the --export-eps option rather than the --export-png option so I'm not sure what's happening internally.

--
Roeland

snibgo
Posts: 12153
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: svg loading ignores -density

Post by snibgo » 2017-03-10T20:00:53-07:00

roelandsch wrote:--export-eps
Well spotted. That's the problem. Telling Inkscape to export to EPS (another vector format) is not a good idea, as IM then has to call Ghostscript to convert from EPS to PNG. Much better to tell IM to export directly to PNG.
snibgo's IM pages: im.snibgo.com

roelandsch
Posts: 20
Joined: 2014-10-27T16:10:38-07:00
Authentication code: 6789

Re: svg loading ignores -density

Post by roelandsch » 2017-03-12T15:19:09-07:00

I still have to find the original issue though.

Some more tests on this other PC:

Code: Select all

>convert -verbose -density 300 test.svg test.png
test.svg SVG 333x333 333x333+0+0 16-bit sRGB 202B 0.016u 0:00.006
test.svg=>test.png SVG 333x333 333x333+0+0 16-bit sRGB 193c 7.86KB 0.031u 0:00.014
Note the resolutions — the SVG file is now treated as a 333×333 image.

Next step — install the latest 6.9.8 version.

Code: Select all

>convert -version
Version: ImageMagick 6.9.8-0 Q16 x64 2017-03-11 http://www.imagemagick.org
...

Code: Select all

>convert -verbose -density 300 test.svg test.png
test.svg SVG 100x100 100x100+0+0 16-bit sRGB 202B 0.016u 0:00.003
test.svg=>test.png SVG 100x100 100x100+0+0 8-bit sRGB 100c 1.66KB 0.000u 0:00.005
A 100×100 image. So this got broken in a recent 6.9.8 version.

--
Roeland

User avatar
fmw42
Posts: 25560
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: svg loading ignores -density

Post by fmw42 » 2017-03-12T16:05:54-07:00

Perhaps an issue with your RSVG renderer, which is one of the delegates used by Imagemagick. You can try Inkscape and see if that works. Either way, see if you can run RSVG or Inkscape directly and get the correct size change when you specify the density. If you do, then perhaps the call from IM to the delegate is not correct. Otherwise, it is an issue with the RSVG or Inkscape delegate and outside the control of Imagemagick.

roelandsch
Posts: 20
Joined: 2014-10-27T16:10:38-07:00
Authentication code: 6789

Re: svg loading ignores -density

Post by roelandsch » 2017-03-12T17:33:51-07:00

RSVG is one of the built-in coders, it's installed with ImageMagick. My guess is that it's embedded in IM_MOD_RL_svg_.dll. How would I call this directly?

Right now on this PC this is working with version 6.9.3-3 and not working with version 6.9.8-0, so it would be a regression somewhere between those two versions. Bug is observed on both Windows 7 and Windows 10, in both cases with clean ImageMagick installations.

User avatar
fmw42
Posts: 25560
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: svg loading ignores -density

Post by fmw42 » 2017-03-12T18:39:36-07:00

Do you have two different versions of RSVG on these two systems? Check by

Code: Select all

convert -list format
and see what it says for SVG

User avatar
fmw42
Posts: 25560
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: svg loading ignores -density

Post by fmw42 » 2017-03-12T18:44:48-07:00

OK I can confirm that is does work in 6.9.3.3 but not in 6.9.8.0 on Mac OSX with RSVG 2.40.16

Code: Select all

convert -density 300 test.svg test.png
identify test.png
test.png PNG 100x100 100x100+0+0 8-bit sRGB 256c 1.48KB 0.000u 0:00.009

im6933 convert -density 300 test.svg test.png
identify test.png
test.png PNG 333x333 333x333+0+0 8-bit sRGB 256c 5.18KB 0.000u 0:00.000

I tested a number of versions and have pinpointed the change to 6.9.6.4 working and 6.9.6.5 not working

It also seems strange to me that one starts with 100x100 pixels and adding -density 300, one gets 333x333? Seems to me that it should have been defaulting to density 72, which would have made the output 100*300/72--> 417x417.

User avatar
fmw42
Posts: 25560
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: svg loading ignores -density

Post by fmw42 » 2017-03-12T19:07:44-07:00

One possibility is that your SVG file is not adequately defined. If I take this SVG file right from the SVG circle document specifications

Code: Select all

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="12cm" height="4cm" viewBox="0 0 1200 400"
     xmlns="http://www.w3.org/2000/svg" version="1.1">
  <desc>Example circle01 - circle filled with red and stroked with blue</desc>

  <!-- Show outline of canvas using 'rect' element -->
  <rect x="1" y="1" width="1198" height="398"
        fill="none" stroke="blue" stroke-width="2"/>

  <circle cx="600" cy="200" r="100"
        fill="red" stroke="blue" stroke-width="10"  />
</svg>
Then using IM 6.9.8.0 Q16 Mac OSX and RSVG 2.40.16, then it does change with -density 300.

Code: Select all

convert test2.svg test2.png
identify test2.png
test2.png PNG 425x142 425x142+0+0 16-bit sRGB 5.74KB 0.000u 0:00.000

convert -density 300 test2.svg test2.png
identify test2.png
test2.png PNG 1417x472 1417x472+0+0 16-bit sRGB 23KB 0.000u 0:00.000
Checking the RSVG versions:

Code: Select all

im6965 convert -list format | grep "SVG"
     MSVG  SVG       rw+   ImageMagick's own SVG internal renderer
      SVG  SVG       rw+   Scalable Vector Graphics (RSVG 2.40.16)
     SVGZ  SVG       rw+   Compressed Scalable Vector Graphics (RSVG 2.40.16)

im6964 convert -list format | grep "SVG"
     MSVG  SVG       rw+   ImageMagick's own SVG internal renderer
      SVG  SVG       rw+   Scalable Vector Graphics (RSVG 2.40.16)
     SVGZ  SVG       rw+   Compressed Scalable Vector Graphics (RSVG 2.40.16)

snibgo
Posts: 12153
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: svg loading ignores -density

Post by snibgo » 2017-03-12T19:11:40-07:00

fmw42 wrote:Seems to me that it should have been defaulting to density 72, which would have made the output 100*300/72--> 417x417.
The default DPI for SVG is 90. 100*300/90 = 333.3333.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25560
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: svg loading ignores -density

Post by fmw42 » 2017-03-12T19:13:49-07:00

Thanks, snibgo, that explains the size issue. I was seeing 333.3333 from my tests by doing 1417/425, but did not know about the 90 dpi default.

roelandsch
Posts: 20
Joined: 2014-10-27T16:10:38-07:00
Authentication code: 6789

Re: svg loading ignores -density

Post by roelandsch » 2017-03-12T21:27:51-07:00

I can confirm that this still works if you specify the width and height in length units. So as a workaround I can specify inches as size, assuming 90 px/inch.

I don't think specifying the size (or other content) in pixels is an error though. Examples on MDN specify sizes in pixels [1]. Inkscape will write out the unit you select in the page properties, and pixels is one of the choices. It should still be possible to load those files with higher resolution than the default.

[1] see for example https://developer.mozilla.org/en-US/doc ... ent/circle:

Code: Select all

<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <circle cx="100" cy="100" r="100"/>
</svg>

Post Reply