5

I want to do something like:

    import image
    image.display_image('http://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg')

And it would come out as an image.

P.S. I want PNG or JPEG, not GIFs.

stovfl
  • 14,998
  • 7
  • 24
  • 51
pythonprogrammer
  • 693
  • 2
  • 7
  • 7
  • 2
    You could use tkinter to display an image on a canvas. Similar [question](http://stackoverflow.com/questions/16424091/python-tkinter-gif-image-in-a-canvas) – Reko Mar 22 '14 at 20:19

1 Answers1

5

This question is somewhat old, but as info on how to do this easily isn't easy to find online, I'm posting this answer in hope it can be useful to others in the future.

To raster a SVG and put it into a ImageTk.PhotoImage object you can do this inside a class used for a tkinter gui:

def svgPhotoImage(self,file_path_name):
        import Image,ImageTk,rsvg,cairo
        "Returns a ImageTk.PhotoImage object represeting the svg file" 
        # Based on pygame.org/wiki/CairoPygame and http://bit.ly/1hnpYZY        
        svg = rsvg.Handle(file=file_path_name)
        width, height = svg.get_dimension_data()[:2]
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height))
        context = cairo.Context(surface)
        #context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
        svg.render_cairo(context)
        tk_image=ImageTk.PhotoImage('RGBA')
        image=Image.frombuffer('RGBA',(width,height),surface.get_data(),'raw','BGRA',0,1)
        tk_image.paste(image)
        return(tk_image)

Then display the image on a Frame widget (e.g. mainFrame) this way:

tk_image=self.svgPhotoImage(filename)
mainFrame.configure(image=tk_image)

If you want to save the picture you can return image instead of tk_image and save it this way:

image.save(filename) # Image format is autodected via filename extension
JeanOlivier
  • 302
  • 2
  • 17
  • can you licence the code under the MIT licence, please? – Shlomi Fish Apr 27 '19 at 18:23
  • 2
    Like all SO posts, it should be licensed under Creative Commons Attribution-Share Alike. See https://stackoverflow.com/help/licensing – JeanOlivier Apr 30 '19 at 21:56
  • How do you install rsvg and cairo? – Andereoo May 06 '19 at 10:17
  • 1
    `pip install pycairo` For RSVG it's harder to answer as it's highly dependent on your platform. Google is your friend here. [Here's a minimal example for Windows](https://www.cairographics.org/cairo_rsvg_and_python_in_windows/). – JeanOlivier May 09 '19 at 18:22
  • @JeanOlivier: thanks for the reply. Thing is CC-by-sa and the GPL, which its newer versions can be sublicensed to, is more restrictive than MIT/expat. StackO has made the code on recent posts under the MIT licence, but your post is too old for that, so I need your explicit approval. Thanks! – Shlomi Fish May 13 '19 at 19:22
  • I give my personal approval for the code excerpt that I've written in the parent answer to be considered licensed under the MIT license. However, you should look into the sources I cited therein to ensure compliance in your own project. I'm not responsible of any misuse of the aforementioned code from any person or organisation. – JeanOlivier May 14 '19 at 20:37
  • @JeanOliver I get a blank window. Any idea what is going wrong? For example, the below code should convert the svg into a png but it creates a blank file instead. ```import cairo; from rsvg import *; 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")``` – Andereoo Jun 06 '19 at 12:44
  • The main difference I see between your code and mine, is that my handle is a `rsvg.Handle` whereas yours is a `rsvghandler` which I assume is imported from the rsvg lib. Maybe try and replace `handle= rsvghandler.Handle('example.svg')` by `handle = Handle('example.svg')`? – JeanOlivier Jun 11 '19 at 19:48