我正在处理一个返回Gtk.Image对象的外部api。 我想将Gtk.Image写入一个文件。 为了说明我已经创建了下面的代码的问题:python gir/Gtk.Image:为什么会破坏我的图像?
from gi.repository import Gtk
import cStringIO
i = Gtk.Image()
i.set_from_file('/home/leon/Pictures/msn/ikea.jpg')
p = i.get_pixbuf()
f = open('/tmp/test.png', 'wb+')
def write(*args, **kwargs):
f.write(args[0])
return True
# ubuntu 11.10 doesn't have pixbuf.save_to_callback so instead use save_to_callbackv
p.save_to_callbackv(write, None, "png", [], [])
f.close()
的文件被写入磁盘,但已损坏。为什么它损坏?
你可能想要写的二进制文件。使用'wb'。 – 2012-02-05 16:59:10
当使用'wb'时,我得到相同的错误:( – Leon 2012-02-05 17:19:48
你得到一个错误?或者你得到一个损坏的图像?图像属性(大小,颜色深度等)都正确,只是内容是乱码?他们搞砸了吗?我不知道这个库,所以只是寻找线索。 – KobeJohn 2012-02-05 17:29:13