Can you display an image inside of a python program (without using pygame)?

image, python, rsvg, svg, tkinter

Solution

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

Problem

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.

Original source

Related problems