我正在處理一個返回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