Python SVG converter creates empty file

早过忘川 提交于 2020-01-14 08:44:08

问题


I have some code below that is supposed to convert a SVG image to a PNG. It runs without errors but creates a PNG file that is blank instead of one with the same image as the original SVG. I did find that it is not an error with cairo but more one relating to rsvg, which I got here.

import cairo
import rsvg

img = cairo.ImageSurface(cairo.FORMAT_ARGB32, 640,480)
ctx = cairo.Context(img)
handle= rsvghandler.Handle('example.svg')
handle.render_cairo(ctx)
img.write_to_png("svg.png")

I am using Python 3.6 on Windows 10.

I can't for the life of me figure out why it isn't displaying the correct picture. Any help would be hugely appreciated.


回答1:


If your goal is to convert from SVG to PNG, I would recommend using Wand, as in the following script:

from wand.api import library
import wand.color
import wand.image

with wand.image.Image() as image:
    with wand.color.Color('transparent') as background_color:
        library.MagickSetBackgroundColor(image.wand, 
                                         background_color.resource) 
    image.read(blob=NAMEOFTHEFILE.read(), format="svg")
    png_image = image.make_blob("png32")

with open(NAMEOFTHENEWFILE, "wb") as out:
    out.write(png_image)


来源:https://stackoverflow.com/questions/56463524/python-svg-converter-creates-empty-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!