6

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.

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
Andereoo
  • 834
  • 1
  • 8
  • 24
  • I tried to reproduce your issue, but I can't seem to install `cairo`. When running `pip install cairo`, I get `Could not find a version that satisfies the requirement cairo (from versions: )`. When running `pip install pycairo`, I get the error `Package cairo was not found in the pkg-config search path.Perhaps you should add the directory containing 'cairo.pc' to the PKG_CONFIG_PATH environment variable`. How did you get it? – physicalattraction Jun 10 '19 at 10:05
  • I installed cairo by downloading the Pycairo whl file at https://www.lfd.uci.edu/~gohlke/pythonlibs/ using `pip install [wheelname]` to install it. I don't remember which one worked for me, but I had to try a few before being able to install it correctly. – Andereoo Jun 10 '19 at 15:01

1 Answers1

4

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)
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
  • Thanks for the answer. The only issue is that I am using cairo so that I can convert the svg to a png and display the new image in a tkinter screen **without saving it**. Is there a way to do this with wand? – Andereoo Jun 08 '19 at 10:56
  • @Andoo I didn't see reference to that in your question. I would suggest that you maybe post a different question. But one way that you can do that is by using the file that you saved and open it in tkinter. [Here you have an example of code to open in tkinter](https://www.c-sharpcorner.com/blogs/basics-for-displaying-image-in-tkinter-python). – Gonçalo Peres Jun 08 '19 at 11:09
  • 1
    I didn't realize that deleting the last two lines will prevent it from saving but still hold the PNG as a variable. I guess that is the answer to my other comment. Also, do you know any way of making my code work? (cairo is really fast) – Andereoo Jun 08 '19 at 11:18
  • How do I install ImageMagick? – Andereoo Jun 10 '19 at 15:07