Convert SVG to PGM and keep whitespace around image

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
chw
Posts: 5
Joined: 2018-07-05T23:38:55-07:00
Authentication code: 1152

Convert SVG to PGM and keep whitespace around image

Post by chw »

I am trying to convert an SVG file to an PGM file using the command line tools.
An example image is here: https://www.dropbox.com/sh/din9ih3llfi7 ... rczSa?dl=0
The initial SVG image is initial.svg and I want to achieve the same result as good.pgm.
I am using the command

Code: Select all

convert -depth 8 -compress none initial.svg output.pgm
However this results in bad.pgm, where the whitespace/viewbox surrounding the image gets cropped/trimmed away.
Using the C++ library it works as expected, and I get the same as good.pgm.
I have tried using both -size, -resize or -extent with the correct dimensions but they also result in the wrong output.
Version information:
convert -version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2018-06-11 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert SVG to PGM and keep whitespace around image

Post by snibgo »

I get a "good" result when using IM v6.9.5-3 and Inkscape v0.91 as the delegate.
snibgo's IM pages: im.snibgo.com
chw
Posts: 5
Joined: 2018-07-05T23:38:55-07:00
Authentication code: 1152

Re: Convert SVG to PGM and keep whitespace around image

Post by chw »

Thank you, I tried removing my install and reinstalling and it works now.
chw
Posts: 5
Joined: 2018-07-05T23:38:55-07:00
Authentication code: 1152

Re: Convert SVG to PGM and keep whitespace around image

Post by chw »

I think I was too quick to accept it. If I do not have inkscape installed it works as intented. However after installing inkscape it "fails" again.
So if I do

Code: Select all

sudo apt-get remove inkscape
and then

Code: Select all

convert -depth 8 -compress none initial.svg output.pgm
It works, but if i then do

Code: Select all

sudo apt-get install inkscape
and then

Code: Select all

convert -depth 8 -compress none initial.svg output.pgm
It fails
My inkscape version is

Code: Select all

inkscape --version
Inkscape 0.92.3 (unknown)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert SVG to PGM and keep whitespace around image

Post by snibgo »

What happens when you use Inkscape directly, instead of via ImageMagick? I suspect this gives the same result. If that's not the result you want, either your SVG is bad or Inkscape has a bug.
snibgo's IM pages: im.snibgo.com
chw
Posts: 5
Joined: 2018-07-05T23:38:55-07:00
Authentication code: 1152

Re: Convert SVG to PGM and keep whitespace around image

Post by chw »

I will try that.

Is it possible to force magick to use another delegate than inkscape? I suppose it uses something internal when inkscape is not installed?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert SVG to PGM and keep whitespace around image

Post by snibgo »

IM always includes MVG. It usually also contains RSVG as an internal ("built-in") delegate. If Inkscape is on the system path, that is the default.

The prefix "msvg:" forces the use of MVG, even if RSVG and Inkscape are available. For some SVG files, MVG works well and is fast.
snibgo's IM pages: im.snibgo.com
chw
Posts: 5
Joined: 2018-07-05T23:38:55-07:00
Authentication code: 1152

Re: Convert SVG to PGM and keep whitespace around image

Post by chw »

I tried forcing msvg but i get the error

Code: Select all

convert -depth 8 -compress none msvg:initial.svg output.pgm
convert: not authorized `/tmp/magick-31003z3lTVNp7uknr' @ error/constitute.c/ReadImage/412.
convert: no images defined `output.pgm' @ error/convert.c/ConvertImageCommand/3210.
also I tried inkscape commandline to png which seems to work.
I have included a Minimal Working Example here
https://www.dropbox.com/sh/lk4vsmblmibw ... 33Mma?dl=0
initial.svg is a very minimal example
inkscape.png is converted using

Code: Select all

inkscape -f initial.svg -e inkscape.png
convert_good.png is converted using

Code: Select all

convert -depth 8 -compress none initial.svg convert_good.png
without inkscape installed
convert_bad.png is converted using

Code: Select all

convert -depth 8 -compress none initial.svg convert_bad.png
with inkscape installed
Post Reply