问题:哪个可重复的过程可以使Windows Python用户将SVG图像渲染为PNG?
许多问题/答案(例如Python中将SVG转换为PNG和Python中服务器端SVG到PNG(或其他图像格式),由于下面解释的原因它们不是重复问题)解释了如何使用Python将SVG转换为PNG。
很遗憾,它们都不能直接用于Python+Windows。经过20多分钟的尝试后,我仍然无法做到。更多关于失败尝试的细节:
Installing
cairo
on Windows is not straightforward, we have to use Gohlke's binaries Intalling pycairo with Python 3.7 on Windows :pip install pycairo-1.20.0-cp37-cp37m-win_amd64.whl
Even once
cairo
is installed,rsvg
(from main answers of Server-side SVG to PNG (or some other image format) in python, Convert SVG to PNG in Python) is not available for Windows:pip install rsvg # or pyrsvg > ERROR: No matching distribution found for pyrsvg
Solutions with
svglib
orreportlab
don't work out-of-the-box on Python3 + Windows:from svglib.svglib import svg2rlg from reportlab.graphics import renderPDF, renderPM drawing = svg2rlg("a.svg") renderPM.drawToFile(drawing, "file.png", fmt="PNG")
Indeed:
AttributeError: 'Image' object has no attribute 'fromstring'
width=1000px
)的PNG导出功能。(当然,SVG可以无限缩放而不会失去质量,但PNG就不是这样了,但我可以接受这一点)。 - Basjsvglib
的例子在 Windows 上对我有效,我无法重现你的错误。你遇到的问题是特定于某些 SVG 文件吗? - Luke Woodward