在Windows上使用Python将SVG转换为PNG [英] Convert SVG to PNG with Python on Windows
问题描述
问题:哪种可重现的过程可以使Windows Python用户将SVG图像渲染为PNG?
许多问题/答案(例如将SVG转换为Python中的PNG 和服务器端SVG转换为PNG(或其他某种图像格式)(由于以下原因而不能重复),解释了如何使用Python将SVG转换为PNG.
Many questions/answers (such as Convert SVG to PNG in Python and Server-side SVG to PNG (or some other image format) in python, which are not duplicates for the reasons explained below) explain how to convert a SVG to PNG with Python.
不幸的是,它们都不是可立即用于Python + Windows 的.经过20多分钟,以及许多不同的尝试,我仍然无法做到这一点.有关失败尝试的更多详细信息:
Unfortunately, none of them are ready-to-use for Python + Windows. After more than 20 minutes, and many different attempts, I'm still unable to do it. More details about failing attempts:
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
即使安装了 cairo
, rsvg
(来自将SVG转换为Python中的PNG ):
pip install rsvg # or pyrsvg
> ERROR: No matching distribution found for pyrsvg
使用 svglib
或 reportlab
的解决方案在Python3 + Windows上无法即用:
Solutions with svglib
or reportlab
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")
确实:
AttributeError: 'Image' object has no attribute 'fromstring'
因此,针对Windows的解决方案会有所帮助.
So a solution - specific for Windows - would be helpful.
推荐答案
根据评论,解决方案是安装 svglib
版本1.0.1和 reportlab
3.5.59
From the comments, the solution was to install svglib
version 1.0.1 and reportlab
3.5.59.
这篇关于在Windows上使用Python将SVG转换为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!