試用了一下 svglib,功能都正常,滿好的,唯一的缺點就是在處理大量的檔案時,就可以看的出來效能沒有很好,呵呵。跑了好久,檔案都還沒有轉完,似乎使用其他原生系統的轉換程式效果會好一點。
svglib:
https://pypi.org/project/svglib/
github:
https://github.com/deeplook/svglib
看了一下github 的 issue,似乎還是有一些異常的情況會發生。
使用方法:
from svglib.svglib import svg2rlg from reportlab.graphics import renderPDF, renderPM drawing = svg2rlg("file.svg") renderPM.drawToFile(drawing, "file.png", fmt="PNG")
有挑戰使用 imagemagick
來換,可是試出來的結果,svg 轉出來是純白的,不知為何?
ImageMagick convert a SVG file results a blank image
The problem here is that I don’t know how ImageMagick uses the librsvg interface in detail. But a few things stand out in the latest librsvg doc. While the paragraphs were only added in the v2.42 doc, I think they apply also to older versions, according to what I have observed in Gnome systems.
When processing an SVG, librsvg will only load referenced files if they are in the same directory as the base file, or in a subdirectory of it…This is so that malicious SVG files cannot include files that are in a directory above.
If you already have SVG data in memory, you can create a memory input stream…Note that in this case, it is important that you specify the base_file for the in-memory SVG data. Librsvg uses the base_file to resolve links to external content, like raster images.
Depending on the ImageMagick implementation, that leads to two possible solutions:
- Load the SVG directly from a file with
$im->readImage('src.svg');
- Use the string, but set the second file argument in
$im->readImageBlob('<?xml version="1.0"?>' . $svg, 'src.svg')
Only as an aside, $im->setFormat('RSVG')
doesn’t make any sense, since “RSVG” is only the name of the library and not a file format.
服用下面的指令,一樣無效:
magick MSVG:test.svg test2.png
改用這個 svgexport 看看:
https://github.com/shakiba/svgexport
Installation
npm install svgexport -g
Usage
svgexport <input file> <output file> <options>
svgexport <datafile>
<options> [<format>] [<quality>] [<input viewbox>] [<output size>] [<resize mode>] [<styles>]
<format> png|jpeg|jpg
If not specified, it will be inferred from output file extension or defaults to "png".
<quality> 1%-100%
<input viewbox> <left>:<top>:<width>:<height>|<width>:<height>
If input viewbox is not specified it will be inferred from input file.
<output size> <scale>x|<width>:<height>|<width>:|:<height>
If output size is specified as width:height, <viewbox mode> is used.
<viewbox mode> crop|pad
Crop (slice) or pad (extend) input to match output aspect ratio, default mode is "crop".
<datafile> Path of a JSON file with following content:
[ {
"input" : ["<input file>", "<option>", "<option>", ...],
"output": [ ["<output file>", "<option>", "<option>", ...] ]
}, ...]
Input file options are merged with and overridden by output file options.
Instead of a JSON file, a Node module which exports same content can be provided.
也可以使用 brew install svg2png
Usage: svg2png [OPTIONS] [<SVG_file> [<PNG_file>]] svg2png - svg2png - Render an SVG image to a PNG image -w, --width=WIDTH Width of output image in pixels -h, --height=HEIGHT Height of output image in pixels -s, --scale=FACTOR Scale image by FACTOR --flipx Flip X coordinates of image --flipy Flip Y coordinates of image --help Give this help list -V, --version Print program version
如果是使用 macOS 也可以使用:
Or without installing anything:
qlmanage -t -s 1000 -o . picture.svg
It will produce picture.svg.png that is 1000 pixels wide.
I have tested it only on OS X 10.6.3.
處理多個檔案,可以試看看這個指令:
find . -name "*.svg" -exec qlmanage -t -s 1000 -o . {} \;