Is there a way to convert svg to jpg or png
Hi everyone,
Is there a way to convert svg to jpg or png? thanks.
Comments
In the past have used Apache Batik https://xmlgraphics.apache.org/batik/
The scenario was that a message contained a diagnostic background image with layered SVG annotations but the receiving system needed to receive a flattened PNG image instead. Can also generate TIFF or JPEG instead.
Batik is a Java based integration, so depends if IRIS integrating with Java is an option for your deployment.
Interesting if someone suggests a reliable Python alternative.
Other directions might be phantom NodeJS / image magic.
StackOverflow suggests using svglib and reportlab to achieve this with python:
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
drawing = svg2rlg("my.svg")
renderPM.drawToFile(drawing, "my.png", fmt="PNG")ImageMagick is likely available for your platform and can be called using $ZF(-100). It has a LOT of image conversion options.
A sample command line for svg to png conversion:
$ convert -background none -density 1000 -resize 1000x myvector.svg myraster.png
Example using $ZF(-100):
Class User.Util.Image [ Abstract ]
{
ClassMethod Convert(pSourceFile As%String, pDestFile As%String, pDensity As%Integer = 1000, pResize As%String = "1000x", pBackground As%String = "none") As%Status
{
Set OsCmd = "/usr/bin/convert"Set OsArgs(1) = "-background"Set OsArgs(2) = pBackground // "none" for transparent and black for formats w/o alpha channelSet OsArgs(3) = "-density"Set OsArgs(4) = pDensity // set the vector width before resizing for best image qualitySet OsArgs(5) = "-resize"Set OsArgs(6) = pResize // image output width/height (default is width 1000 keeping aspect ratio)Set OsArgs(7) = pSourceFile
Set OsArgs(8) = pDestFile // file type controlled by extension; .png, .jpg, .gif etc.Set OsArgs = 8Set tRC = $ZF(-100,"",OsCmd,.OsArgs)
// On Linux, a return code of 0 indicates successIf '(tRC = 0)
{
Return$$$ERROR(5001,"OsCmd "_OsCmd_" Returned Error Code ["_tRC_"]")
}
Return$$$OK
}
}
Called like this:
Set tSC = ##class(User.Util.Image).Convert("/path/to/filename.svg", "/path/to/filename.png")Appreciated the help @Jeffrey Drumm @Julian Matthews @Alex Woodhead . It seems that we need the third-part tool to achieve this.