2013-10-18 140 views
3

正如我在PyQt的容易做这样的:绘制SVG图像在python

img = '''<?xml version="1.0" encoding="UTF-8"?> 
<svg width="50" height="50" xmlns="http://www.w3.org/2000/svg"> 
<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ --> 
<g> 
    <title>Layer 1</title> 
    <circle stroke-width="3" stroke="#1a1a1a" fill="#dfdbd2" r="16" cy="25" cx="25"/> 
    <path stroke-width="0" fill="#1a1a1a" d="m25,9a16,16 0 0 0 0,32l0,-1.5a18,18 0 0 0 0,-29l0,-1.5z"/> 
</g> 
</svg>''' 
image = QtCore.QByteArray(img) 
self.svgwidget.load(image) 

我怎样才能GTK中做到这一点?有任何想法吗? :) 提前致谢!

+0

对不起,因为糟糕的英语:( –

+0

谷歌五秒钟:http://faq.pygtk.org/index.py?req=show&file=faq08.010.htp –

+0

谢谢!,但这个链接是关于gtk2,它根本不涉及这个问题。 –

回答

9
stream = Gio.MemoryInputStream.new_from_bytes(GLib.Bytes.new(img)) 
pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, None) 
self.svgwidget = Gtk.Image.new_from_pixbuf(pixbuf) 

或者使用RSVG库,如果你不介意额外的依赖,并希望做的SVG花哨的东西,一旦你已经装好了:

svg = Rsvg.Handle.new_from_data(img) 
pixbuf = svg.get_pixbuf() 
svgwidget = Gtk.Image.new_from_pixbuf(pixbuf) 
+0

非常感谢!这正是我一直在寻找的:) –

8

的人谁愿意来加载并使用GTK 3和Python缩放SVG图像(同时保留SVG图像质量!):

from gi.repository import Gtk, GdkPixbuf 

path = "/path/to/image.svg" 
width = -1 
height = 128 
preserve_aspect_ratio = True 

pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, width, height, preserve_aspect_ratio) 
image = Gtk.Image() 
image.set_from_pixbuf(pixbuf) 

参见https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-File-Loading.html#gdk-pixbuf-new-from-file-at-scale