问题:哪个可重现的过程可以使 Windows Python 用户将 SVG 图像渲染为 PNG?
许多问题/答案(例如 Convert SVG to PNG in Python 和 Server-side SVG to PNG (or some other image format) in python ,由于下面解释的原因,它们不是重复的)解释了如何使用 Python 将 SVG 转换为 PNG。
不幸的是,它们中没有一个可用于 Python + Windows .经过 20 多分钟和许多不同的尝试,我仍然无法做到。
有关失败尝试的更多详细信息:
cairo
在 Windows 上并不简单,我们必须使用 Gohlke 的二进制文件 Intalling pycairo with Python 3.7 on Windows :pip install pycairo-1.20.0-cp37-cp37m-win_amd64.whl
cairo
已安装,rsvg
(来自 Server-side SVG to PNG (or some other image format) in python 、 Convert SVG to PNG in Python 的主要答案)不适用于 Windows:pip install rsvg # or pyrsvg
> ERROR: No matching distribution found for pyrsvg
svglib
的解决方案或 reportlab
不要在 Python3 + Windows 上开箱即用:from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF, renderPM
drawing = svg2rlg("a.svg")
renderPM.drawToFile(drawing, "file.png", fmt="PNG")
的确:AttributeError: 'Image' object has no attribute 'fromstring'
因此,特定于 Windows 的解决方案会有所帮助。
最佳答案
根据评论,解决方案是安装 svglib
版本 1.0.1 和 reportlab
3.5.59。
关于python - 在 Windows 上使用 Python 将 SVG 转换为 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65759849/