以给定的分辨率将 SVG 图像“烘焙”为 PNG?

Naf*_*Kay 24 linux png svg

我在 Linux 上,我想将一个 SVG 文件以给定的分辨率(缩放)烘焙成一个 PNG 文件。有没有办法从命令行快速有效地做到这一点?

我正在寻找这样的东西:

svg2png --width 128 --height 128 input.svg output.png
Run Code Online (Sandbox Code Playgroud)

有这个工具吗?

Mih*_*aru 25

使用ImageMagick 的 convert

convert -resize 128x128 input.svg output.png
Run Code Online (Sandbox Code Playgroud)

如果输入画布大小小于给定大小,这将放大输出而不重新采样。要获得更高质量的结果,请添加-density参数:

convert -density 500 -resize 128x128 input.svg output.png
Run Code Online (Sandbox Code Playgroud)

ImageMagick 将矢量图像光栅化为其画布分辨率,默认密度为 72 dpi。
要通过适当的采样进行放大,您可以指定一个过高的值,例如上述示例中的 500 dpi。

对于大尺寸画布,使用过高的光栅密度可能会导致性能问题。最低和理想的栅格密度是: raster_density = raster_size / original_size * original_density

这种理想的光栅密度将直接将图像输出到预期的大小(-resize不需要)。但由于可能会出现浮点数近似值,建议使用稍高的值和调整大小以获得像素完美的结果。

您可以使用ImageMagick 的百分比转义获得原始大小和密度:

 convert input.svg -format "%w %h %[resolution.x] %[resolution.y]" info:
Run Code Online (Sandbox Code Playgroud)

请注意,ImageMagick 不处理 SVG 的完整规范。如果在形状渲染不正确时遇到问题,请查看有关选择 ImageMagick SVG 渲染器的线程。


Dan*_* D. 10

双方rsvginkscape可以:

$ rsvg
Usage: rsvg [OPTIONS...] file.svg file.png
  -d, --dpi-x=<float>          pixels per inch
  -p, --dpi-y=<float>          pixels per inch
  -x, --x-zoom=<float>         x zoom factor
  -y, --y-zoom=<float>         y zoom factor
  -w, --width=<int>            width
  -h, --height=<int>           height
  -q, --quality=<int>          JPEG quality
  -f, --format=[png, jpeg]     save format
  -v, --version                show version information

Help options:
  -?, --help                   Show this help message
  --usage                      Display brief usage message
Run Code Online (Sandbox Code Playgroud)

和:

$ inkscape --help
Usage: inkscape [OPTIONS...] [FILE...]

Available options:
  -V, --version                             Print the Inkscape version number
  -z, --without-gui                         Do not use X server (only process
                                            files from console)
  -g, --with-gui                            Try to use X server (even if
                                            $DISPLAY is not set)
  -f, --file=FILENAME                       Open specified document(s) (option
                                            string may be excluded)
  -p, --print=FILENAME                      Print document(s) to specified
                                            output file (use '| program' for
                                            pipe)
  -e, --export-png=FILENAME                 Export document to a PNG file
  -d, --export-dpi=DPI                      The resolution used for exporting
                                            SVG into bitmap (default 90)
  -a, --export-area=x0:y0:x1:y1             Exported area in SVG user units
                                            (default is the canvas; 0,0 is
                                            lower-left corner)
  -D, --export-area-drawing                 Exported area is the entire
                                            drawing (not canvas)
  -C, --export-area-canvas                  Exported area is the entire canvas
      --export-area-snap                    Snap the bitmap export area
                                            outwards to the nearest integer
                                            values (in SVG user units)
  -w, --export-width=WIDTH                  The width of exported bitmap in
                                            pixels (overrides export-dpi)
  -h, --export-height=HEIGHT                The height of exported bitmap in
                                            pixels (overrides export-dpi)
  -i, --export-id=ID                        The ID of the object to export
  -j, --export-id-only                      Export just the object with
                                            export-id, hide all others (only
                                            with export-id)
  -t, --export-use-hints                    Use stored filename and DPI hints
                                            when exporting (only with
                                            export-id)
  -b, --export-background=COLOR             Background color of exported
                                            bitmap (any SVG-supported color
                                            string)
  -y, --export-background-opacity=VALUE     Background opacity of exported
                                            bitmap (either 0.0 to 1.0, or 1 to
                                            255)
  -l, --export-plain-svg=FILENAME           Export document to plain SVG file
                                            (no sodipodi or inkscape
                                            namespaces)
  -P, --export-ps=FILENAME                  Export document to a PS file
  -E, --export-eps=FILENAME                 Export document to an EPS file
  -A, --export-pdf=FILENAME                 Export document to a PDF file
  -T, --export-text-to-path                 Convert text object to paths on
                                            export (EPS)
  -F, --export-embed-fonts                  Embed fonts on export (Type 1
                                            only) (EPS)
  -B, --export-bbox-page                    Export files with the bounding box
                                            set to the page size (EPS)
  -X, --query-x                             Query the X coordinate of the
                                            drawing or, if specified, of the
                                            object with --query-id
  -Y, --query-y                             Query the Y coordinate of the
                                            drawing or, if specified, of the
                                            object with --query-id
  -W, --query-width                         Query the width of the drawing or,
                                            if specified, of the object with
                                            --query-id
  -H, --query-height                        Query the height of the drawing
                                            or, if specified, of the object
                                            with --query-id
  -S, --query-all                           List id,x,y,w,h for all objects
  -I, --query-id=ID                         The ID of the object whose
                                            dimensions are queried
  -x, --extension-directory                 Print out the extension directory
                                            and exit
      --vacuum-defs                         Remove unused definitions from the
                                            defs section(s) of the document
      --verb-list                           List the IDs of all the verbs in
                                            Inkscape
      --verb=VERB-ID                        Verb to call when Inkscape opens.
      --select=OBJECT-ID                    Object ID to select when Inkscape
                                            opens.

Help options:
  -?, --help                                Show this help message
      --usage                               Display brief usage message
Run Code Online (Sandbox Code Playgroud)