Leon
Leon

Reputation: 881

python gir / Gtk.Image: why does it corrupt my image?

I'm working with an external api which returns an Gtk.Image object. I'm trying to write that Gtk.Image to a file. To illustrate the problem I've created the following code:

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()

The file gets written to disk but is corrupted. Why is it corrupted?

Upvotes: 1

Views: 564

Answers (1)

plaes
plaes

Reputation: 32716

Here's a bit shorter (and working) version:

from gi.repository import Gtk

i = Gtk.Image()
i.set_from_file('/home/leon/Pictures/msn/ikea.jpg')

p = i.get_pixbuf()
p.savev('/tmp/xxx.png', 'png', [], [])

Though, I'm not sure what goes wrong with your version :S

Upvotes: 1

Related Questions