如何在Python中将SVG图像渲染为PNG文件?

Duc*_*een 15 python linux svg render

所以我想从具有目标分辨率 WxH 的 python 代码渲染 SVG(将 SVG 文本作为 str,就像我动态生成的那样):

<svg width="200" height="200" viewBox="0 0 220 220"
     xmlns="http://www.w3.org/2000/svg">
  <filter id="displacementFilter">
    <feTurbulence type="turbulence" baseFrequency="0.05"
        numOctaves="2" result="turbulence"/>
    <feDisplacementMap in2="turbulence" in="SourceGraphic"
        scale="50" xChannelSelector="R" yChannelSelector="G"/>
  </filter>

  <circle cx="100" cy="100" r="100"
      style="filter: url(#displacementFilter)"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

转换为 png 图像。如何在Python中做这样的事情?

amg*_*mgg 32

有多种解决方案可用于在 python 中将 svgs 转换为 png,但并非所有解决方案都适合您的特定用例,因为您正在使用 svg 过滤器。

解决方案 过滤器有效吗? 阿尔法通道? 直接从python调用?
开罗夫格 一些* 是的 是的
svglib 是的
墨景 是的 是的 通过subprocess
棍棒 是的 是的 是的

* 来自cairosvg 文档

仅支持feOffset,feBlend和过滤器。feFlood


注意:我已为所有示例图像添加了纯白色背景,以便在深色背景上更容易看到它们,除非上表中另有说明,否则原件确实具有透明背景


开罗夫格

cairosvg 的输出

import cairosvg

# read svg file -> write png file
cairosvg.svg2png(url=input_svg_path, write_to=output_png_path, output_width=width, output_height=height)

# read svg file -> png data
png_data = cairosvg.svg2png(url=input_svg_path, output_width=width, output_height=height)

# svg string -> write png file
cairosvg.svg2png(bytestring=svg_str.encode(), write_to=output_png_path, output_width=width, output_height=height)

# svg string -> png data
png_data = cairosvg.svg2png(bytestring=svg_str.encode(), output_width=width, output_height=height)
Run Code Online (Sandbox Code Playgroud)

svglib

svglib 的输出

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

# read svg -> write png
renderPM.drawToFile(svg2rlg(input_svg_path), output_png_path, fmt='PNG')
Run Code Online (Sandbox Code Playgroud)

墨景

inkscape 的输出

要读取文件作为输入,请将文件的路径作为最后一个参数。要使用字符串作为输入,请添加--pipe参数并将字符串传递到 stdin。

要写入文件作为输出,请将参数--export-filename=+path 添加到输出文件。要直接获取输出内容而不写入文件,请使用--export-filename=-并将其发送到 stdout。

CLI 选项的完整文档位于此处

import subprocess
inkscape = ... # path to inkscape executable

# read svg file -> write png file
subprocess.run([inkscape, '--export-type=png', f'--export-filename={output_png_path}', f'--export-width={width}', f'--export-height={height}', input_svg_path])

# read svg file -> png data
result = subprocess.run([inkscape, '--export-type=png', '--export-filename=-', f'--export-width={width}', f'--export-height={height}', input_svg_path], capture_output=True)
#   (result.stdout will have the png data)

# svg string -> write png file
subprocess.run([inkscape, '--export-type=png', f'--export-filename={output_png_path}', f'--export-width={width}', f'--export-height={height}', '--pipe'], input=svg_str.encode())

# svg string -> png data
result = subprocess.run([inkscape, '--export-type=png', '--export-filename=-', f'--export-width={width}', f'--export-height={height}', '--pipe'], input=svg_str.encode(), capture_output=True)
#   (result.stdout will have the png data)
Run Code Online (Sandbox Code Playgroud)

棍棒

在此输入图像描述

from wand.image import Image
from wand.Color import Color

with Color('#00000000') as bgcolor,\
  # to read input from a file:
  Image(filename=input_svg_path, width=width, height=height, background=bgcolor) as img:
  # or, to use input from a string:
  Image(blob=svg_str.encode(), format='svg', width=width, height=height, background=bgcolor) as img:
    # to save output to a file:
    with img.convert('png') as output_img:
        output_img.save(filename=output_png_path)
    # or, to get the output data in a variable:
    png_data = img.make_blob(format='png')
Run Code Online (Sandbox Code Playgroud)


Cod*_*apy 6

你可以使用CairoSVG

CairoSVG 可在 PyPI 上使用,您可以使用 pip 安装它:

pip3 install cairosvg
Run Code Online (Sandbox Code Playgroud)

在你的代码中:

import cairosvg

width = 640
height = 480
cairosvg.svg2png(url='logo.svg', write_to='image.png', output_width=width, output_height=height)
Run Code Online (Sandbox Code Playgroud)