SVG转图片(Python/Node) - 小右

144 阅读2分钟

前言

今天有一个svg批量转图片的需求,现将实现方法记录一下,以便后续借鉴。

实现方案

Python

依赖安装:

pip3 install svglib

代码

from reportlab.graphics import renderPM
from svglib.svglib import svg2rlg

if __name__ == '__main__':
    svg_path = '/Users/mini-right/Documents/humo/files/MA2053.1.svg'
    png_path = '/Users/mini-right/Documents/humo/files/MA2053.1.png'
    pic = svg2rlg(svg_path)
    renderPM.drawToFile(pic, png_path)

Node

依赖安装:

npm install svgexport -g

命令:

  • 导出图片宽高为960:480 svgexport /Users/mini-right/Documents/humo/files/MA2053.1.svg /Users/mini-right/Documents/humo/files/MA2053.1.png 960:480
  • 比例扩大1.5x svgexport /Users/mini-right/Documents/humo/files/MA2053.1.svg /Users/mini-right/Documents/humo/files/MA2053.1.png 1.5x
  • 输出图片质量为80% svgexport /Users/mini-right/Documents/humo/files/MA2053.1.svg /Users/mini-right/Documents/humo/files/MA2053.1.png 80%

参数详解

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.

问题

svglib

  • 处理比svgexport快很多 但是导出的图片会有变形

原图

MA0005.2.png

处理后

MA0005.2.png

svgexport

  • 处理过程较慢 基本上1.5秒处理一个
  • 会偶现无法启动浏览器进程 暂未知解决办法
/usr/local/lib/node_modules/svgexport/node_modules/puppeteer/lib/launcher/BrowserRunner.js:159
            reject(new Error([
                   ^

Error: Failed to launch the browser process!
Received signal 11 SEGV_MAPERR 000000000000
 [0x00011fb58489]
 [0x00011fa81ff3]
 [0x00011fb583a1]
 [0x7ff81c410e2d]
 [0x00010c621000]
 [0x7ff8271d9ef2]
 [0x00011fb75ae7]
 [0x00011fac9e8d]
 [0x00011fac9d58]
 [0x00011facb506]
 [0x00011faed02b]
 [0x00011fb00b2e]
 [0x00011fb008c0]
 [0x00011fa9ece7]
 [0x00011fb010b4]
 [0x00011faccbf5]
 [0x00011fb20ecc]
 [0x00011fb64b17]
 [0x7ff81c3fb514]
 [0x7ff81c3f702f]
[end of stack trace]


TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md

    at onClose (/usr/local/lib/node_modules/svgexport/node_modules/puppeteer/lib/launcher/BrowserRunner.js:159:20)
    at ChildProcess.<anonymous> (/usr/local/lib/node_modules/svgexport/node_modules/puppeteer/lib/launcher/BrowserRunner.js:150:76)
    at ChildProcess.emit (node:events:402:35)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)