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

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

我在 Linux 上,我想获取一个 SVG 文件并将其以给定的分辨率(缩放)烘焙成 PNG 文件。有没有办法从命令行快速有效地完成此操作?

我正在寻找这样的东西:

svg2png --width 128 --height 128 input.svg output.png

有这个工具吗?

答案1

使用ImageMagick 的转换

convert -resize 128x128 input.svg output.png

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

convert -density 500 -resize 128x128 input.svg output.png

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:

请注意,ImageMagick 无法处理 SVG 的完整规范。如果遇到形状渲染不正确的问题,请查看此主题选择 ImageMagick SVG 渲染器

答案2

rsvg和都inkscape可以:

$ 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

和:

$ 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

相关内容