SVG to monochrome PNG - how to size PNG file?

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
JaredDavis
Posts: 4
Joined: 2015-07-03T12:09:11-07:00
Authentication code: 1151

SVG to monochrome PNG - how to size PNG file?

Post by JaredDavis »

Hi,

I'm trying to convert a svg to a monochrome PNG to be printed on a 203 dpi printer.

Here is a sample SVG that is 2" x 2" with a centered 1" x 1" black rectangle.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" width="144pt" height="144pt" viewBox="0 0 143 143">

<g transform="translate(36,36)"> 
   <rect x='0.00' y='0.00' width='72.00' height='72.00' style='fill:black' />
</g>
</svg>
Here is the command used to create the png

Code: Select all

ImageMagick-6.8.8-7\convert -density 135  testsvg.svg  -monochrome  testsvg.png

The resulting png file has a ragged line for the rectangle.

1) How do I create a smooth line without adding more colors?

2) How does -density relate to output pixel count?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: SVG to monochrome PNG - how to size PNG file?

Post by fmw42 »

How do I create a smooth line without adding more colors?
I am not sure what you mean by the above. Do you mean the borders of the black square? If so, then -monochrome will dither. If you do not want the dither, then use -threshold XX% to convert to b/w. But your svg file is already b/w (at least grayscale due to antialiasing)! So why even bother with -monocrome.



Increase the density by a factor of 4, then resize after reading the svg by 25%

The larger the density the bigger the image in pixels in proportion. So decide what density you want for your output without the resize. Then multiply that density by 4 and then resize by 25% as above.

Normal density is 72. So to get better output resolution, do the following (72*4=288).

Code: Select all

convert -density 288 test.svg -resize 25% -depth 8 -threshold 50% result.png
JaredDavis
Posts: 4
Joined: 2015-07-03T12:09:11-07:00
Authentication code: 1151

Re: SVG to monochrome PNG - how to size PNG file?

Post by JaredDavis »

Thanks for the reply. I still don't get it.

The sample posted did not have all of the issues. Sorry. The real svg has other colors used to create "grey" regions. The printer is a Zebra B&W bar code printer and it does no processing of the image. It is either black or white.

Since I posted, I found a SVG option that sharpened the lines. The sample below has that change.


I'm still confused as to how density of 135 produces a 405 pixel png with a 3 x 3 reported size in the png.

Desired pixels in png = 203 dpi * 2" image size = 406. I can deal with being 1 off.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" width="144pt" height="144pt" viewBox="0 0 143 143"
  version="1.1" 
  shape-rendering="crispEdges"
>

<g transform="translate(10,10)"> 
   <rect x='0.00' y='0.00' width='20.00' height='20.00' style='fill:orange' />
</g>
<g transform="translate(36,36)"> 
   <rect x='0.00' y='0.00' width='72.00' height='72.00' style='fill:black' />
</g>
</svg>
Here are the commands

Code: Select all

\ImageMagick-6.8.8-7\convert -density 135  testsvg.svg  -monochrome  testsvg.png
\ImageMagick-6.8.8-7\identify -verbose  testsvg.png
Here is the output:

Code: Select all

snip \ImageMagick-6.8.8-7\convert -density 135  testsvg.svg  -monochrome  testsvg.png
convert.exe: `%s' (%d) "inkscape" "testsvg.svg" --export-png="C:/Users/JAREDD~1/
AppData/Local/Temp/magick-5424gy1xlBeRPGaC" --export-dpi="135,135" --export-back
ground="rgb(100,100,100)" --export-background-opacity="1" > "C:/Users/JAREDD~1/A
ppData/Local/Temp/magick-5424Bk5yTHwMxD_v" 2>&1 @ error/utility.c/SystemCommand/
2036.

Code: Select all

snip ImageMagick-6.8.8-7\identify -verbose  testsvg.png
Image: testsvg.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 405x405+0+0
  Resolution: 135x135
  Print size: 3x3
  Units: Undefined
  Type: Bilevel
  Base type: Bilevel
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8/1-bit
  Channel depth:
    gray: 1-bit

snip
 
  Properties:
    date:create: 2015-07-03T14:49:50-04:00
    date:modify: 2015-07-04T09:28:15-04:00
    png:bKGD: chunk was found (see Background color, above)
    png:cHRM: chunk was found (see Chromaticity, above)
    png:gAMA: gamma=0.45455 (See Gamma, above)
    png:IHDR.bit-depth-orig: 1
    png:IHDR.bit_depth: 1
    png:IHDR.color-type-orig: 0
    png:IHDR.color_type: 0 (Grayscale)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 405, 405
    png:pHYs: x_res=135, y_res=135, units=0
    png:text: 3 tEXt/zTXt/iTXt chunks were found
    signature: d6e7f5241d439e2719649b526e3815610677561f41000546dd715328120245a3
    svg:base-uri: file:/// snip /test
svg.svg
  Artifacts:
    filename: testsvg.png
    verbose: true
  Tainted: True
  Filesize: 948B
  Number pixels: 164K
  Pixels per second: 10.25MB
  User time: 0.016u
  Elapsed time: 0:01.016
  Version: ImageMagick 6.8.8-7 Q16 x86 2014-02-13 http://www.imagemagick.org
I then tried this:

Code: Select all

convert -density 406  testsvg.svg -resize 50%% -monochrome  testsvg1.png
density = desired dpi * 2 ( x 4 took too long to execute)
resize = 1/2 = 50%

But it produced a png with 1832 pixels vs the desired 406 pixels. The black rectangle no longer has crisp edges.

Code: Select all

mage: testsvg1.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 1832x1832+0+0
  Resolution: 406x406
  Print size: 4.51232x4.51232
  Units: Undefined

This produces a 915 pixel png

Code: Select all

convert -density 203 -size '144x144'  testsvg.svg -resize '406x406' -monochrome  testsvg3.png
Thanks again for your help.
JaredDavis
Posts: 4
Joined: 2015-07-03T12:09:11-07:00
Authentication code: 1151

Re: SVG to monochrome PNG - how to size PNG file?

Post by JaredDavis »

More info:

output of

identify -verbose testsvg.svg

Shows a 90 dpi default density.


Image: testsvg.svg
Format: SVG (Scalable Vector Graphics)
Mime type: image/svg+xml
Class: DirectClass
Geometry: 180x180+0+0
Resolution: 90x90
Print size: 2x2
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: SVG to monochrome PNG - how to size PNG file?

Post by snibgo »

I'm not an SVG expert, but I think the problem is your units in SVG. You give dimensions in two different units: pt and "blank". The are 72 points to the inch. "Blank" represents "user units", which by default is pixels. So SVG software gets as confused as me about what you are asking for.

Why do you have a viewport? You define the x-coords as from 0.0 to 143.0, which is only 143 units. Perhaps you wanted 144.

Why do you specify the width and height in points, but the rectangle in "user units"?

If I were you, I would leave off viewport so it will default to 100% of width and height. I'd leave off all units. Then "-density" will (probably) work as you would expect.

I should add: this will give you an image with the required number of pixels. If downstream needs a physical size, adjust "-density" after reading the SVG file.
snibgo's IM pages: im.snibgo.com
JaredDavis
Posts: 4
Joined: 2015-07-03T12:09:11-07:00
Authentication code: 1151

Re: SVG to monochrome PNG - how to size PNG file?

Post by JaredDavis »

THANKS!

I thought the SVG was based on 72 user units in an inch, not 90px per inch.

144/90 = 1.6inches * density = pixels in png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: SVG to monochrome PNG - how to size PNG file?

Post by snibgo »

Default SVG resolution is 90 dpi.

For more about SVG, see http://www.w3.org/TR/SVG/
snibgo's IM pages: im.snibgo.com
Post Reply