ImageMagick viewbox when converting SVG - svg

I'm trying to convert an SVG file to a PNG using convert, where the input image has SVG artifacts outside the document border - this is a byproduct of sloppy artistry, and is convenient when drawing a pretty picture.
However, when I try to use ImageMagick to convert the SVG file to a rasterized PNG, convert keeps the artifacts outside the border in the result, and recalculates the SVG viewbox. This looks very ugly, and is not what I would like as a result. For instance, I would run the following command:
convert input.svg -depth 200 -units pixelsperinch -border 11inx17in output.png
The SVG document clearly has the border and viewbox defined:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
id="svg2"
viewBox="0 0 990.00002 1530"
height="17in"
width="11in">
...
Yet convert treats the entire SVG document (artifacts and all) as the input image, recalculates the viewbox, and the resulting output is slightly skewed, in addition to the extra artifacts spilling outside the document border.

Related

How to convert SVGs to GIFs with ImageMagick?

I'd like to convert a SVG that has a reference to a GIF inside into a final GIF.
The SVG shows fine with the GIF in a browser, but converting the file from SVG to GIF results in only converting the first frame of the GIF.
that's my test.svg:
<svg id="TEST" data-name="TEST" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1000" height="1000" viewBox="0 0 2000 2000">
<image width="2000" height="2000" xlink:href="input.gif"/>
</svg>
imagemagick command:
magick convert test.svg output.gif
the result is that only the first frame gets converted into a GIF, not the whole sequence.
Anything I can adjust with the ImageMagick command to make this work?

How to convert SVG to BMP?

This question may have been answered in multiple websites. However, I am having issues getting it to work correctly.
My goal is to convert an SVG file to BMP using command line. Below is the content of a simple SVG file. Note the resolution must be 912x1140px. Thus the resulting BMP file must be 912x1140px as defined in the SVG.
<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="full" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink" width="912px" height="1140px" preserveAspectRatio="none" viewBox="0,0,9.855,6.1614"><defs />
<rect fill="#FFF" height="100%" width="100%" x="0" y="0" />
<rect x="4" y="2.5" width="1.0" height="1.0" fill="#000" />
</svg>
When I use GIMP using GUI, the resulting BMP file is correct as expected having a size of 912x1140px. The size of the file is ~3.96MB. However I must use the GUI to open my SVG file and manually convert it to BMP. I prefer to use command line because I have a batch of SVG files I want to convert.
When I use ImageMagick to convert, the resulting BMP file gets resized to 855x1069px instead of staying at 912x1140px. The resulting BMP file ~3.48MB: convert myfile.svg myfile.bmp
When I use rsvg-convert, the resulting BMP file is correct 912x1140 but its around 7KB. I am unsure if rsvg-convert even supports BMP format because according to the docs, BMP is not listed as the support format. Thus this leads me to question if the convert BMP file is even correct. rsvg-convert -o myfile.bmp myfile.svg
When I use command line Inkscape, the resulting BMP file is correct resolution of 912x1140, but size is around 6.51KB. I am unsure if this is correct since the BMP file generated using GIMP results in a file size of 3.96MB. Secondly, Inkscape GUI doens't have any options to export to BMP. In addition, it defaults to 32 bits. I want it to be 24 bit depth. inkscape -z myfile.svg -e myfile.bmp

svg + inkscape + unwanted cropping

Given the following test.svg file
<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="tiny"
height="5cm" version="1.2"
viewBox="-1 -1 2 2"
width="5cm" xmlns="http://www.w3.org/2000/svg"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs />
<rect fill="blue" height="1" width="1" x="0" y="0" />
</svg>
using the command
inkscape -D -z --file=test.svg --export-pdf=text.pdf --export-latex
I get a pdf of 25mm x 25mm. The proportions are correct (I use only half the width and half the height), my question is how do avoid an automatic crop of the picture. I need a PDF of 50mm x 50mm, with only the left upper square filled.
In the inkscape documentation for the option -D (that I use) there is the comment without margins or cropping that is exactly what I need. The result is as described above, cropped.
man inkscape shows the following explanation for -D:
In SVG, PNG, PDF, PS, and EPS export, exported area is the drawing (not page), i.e. the bounding box of all objects of the document...
That is what you describe as cropping. For -C it says:
In SVG, PNG, PDF, PS, and EPS export, exported area is the page. This is the default for SVG, PNG, PDF, and PS, so you don't need to specify this...
So, simply leave off the -D option.

How Do I export an SVG without it getting an <image> element and not a <path>

I keep exporting a PSD element with a clear background as an SVG and I continue to get
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="0.972in" height="0.861in">
<image x="0px" y="0px" width="70px" height="62px" xlink:href="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAAA+CAMAAAChgZ0aAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAACfFBMVEUnKzT///8nKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQnKzQ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HVZNskyJWWoRo/fSpitFhvg0w1iIpFeaOcXNiXq5sQs5k/bAHZYuLy1hpZeRsOQqkGarfzhx3yGKibvnGnvFqo4z83aWAXv423guJSb2vtbuyccgp92pgG2MNvE1VgzHzu6PfCPr5qM1VgavbzdjTCMP6lD6jAaA5yTDQUM3bITw1mE8fUA78c3qkGk0dLiV/6ZanB4Ho4AkmM6YxqME0c4w8GxiKGqsEc5ZhjUMavK1RgLLg410Bvfj2uAnMijbHEZtrgwmuUY3CjPZkFp4L4ZD6tmFKFqWeOBqrC+b1aMaYOt79mvm0V8YczIUoxuMm24O5nxI3nrEIKpYMdlBnOYbvOK6JYcbqsqyTMEMx2ZaFKMBewMa1CusN9lJUo6VIx2hN7Dr+InEs+U8bhV00PsWMOBGKOvewjxY8s0S4HY9KKrdNd8YkyGxMcq3aySccwdLWrLxjc9fgycDZtmGfYNbP3lOv0x6Uu3q/yBoZv1npL8cfqumaRE7W0oHDLy1WRRz7jNoh98Z27KN2b4AWk8j5ZRTLGInttfIBisufcZ91KZq4OJDFQSP0yPPRAmU/VEgVHKcaAhczio8duKU/IIwdWgCwGssjDpj11Q6kg29rzGbjBCAPJnstuzgXlqL9on6jSGNp9+LKVSesvg8iI54AnDJiSsGaA5MJ4Rf+x0jFZy2HgtQHrHn4jEiy2Y8xbp6AsBk5fpWPZApdwCM3Pd43gJQZq39Oh44NTMJMOd4la8BoD1o/U/E9V7aFs8tnru4MPGIDFxPm8uu29iIbogdjeucdAxjv8XR/hgPuFqJES/tkDBlrT7QNmiZQYIi8xkEqHuthsaPhK5+BvoAgDZhow/Xc6VRpkvJRnDFib7GfuHz9BMQYgX6D8+g1qMFBEA/ZH3oB7h4G/98P/DXKj2zD/AZ/Mbsp7NMuGAAAAAElFTkSuQmCC" />
</svg>
I would like to export this as a <path> element instead of <image>
this is the PNG of what that SVG is:
I have converted already tried to image trace this on illustrator but the path gets sloppy.
Thanks,
Zlerp
Sorry but Photoshop just doesn't do SVGs like that. SVG is a vector format and the only things in photoshop that are vectors are paths and selections and neither of those have things like line thickness and color so they'd be invisible as an image.
That's what Illustrator is for. Or for a free option, there's InkScape.

Center a character in svg

I'm looking to produce a SVG file containing a single ascii character (say 'g' or 'W') from an arbitrary font centered within a 100 x 100 window. This is the closest I've got, but it's not quite right...
<?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 viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg" version="1.1">
<text x="50" y="50" font-size="100px"
text-anchor="middle" alignment-baseline="middle">g</text>
</svg>
SVG does not have automatic vertical layout capabilities on its own; you need something else to compute the height of the box, like JavaScript or…
Looks like you're embedding this inside of a web browser. If so, then you can use the automatic layout capabilities of HTML to do this for you, using <foreignObject> to embed an HTML snippet inside your SVG with display:table-cell; vertical-align:middle on the content.
There is no general way to centre text vertically in SVG.
The best you can to is work out where the baseline (<text> y attribute) should go for a particular font and group of characters (eg. all capitals).

Resources