Scale an image in GTK Scale an image in GTK python python

Scale an image in GTK


Load the image from a file using gtk.gdk.Pixbuf for that:

import gtkpixbuf = gtk.gdk.pixbuf_new_from_file('/path/to/the/image.png')

then scale it:

pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)

Then, if you want use it in a gtk.Image, crate the widget and set the image from the pixbuf.

image = gtk.Image()image.set_from_pixbuf(pixbuf)

Or maybe in a direct way:

image = gtk.image_new_from_pixbuf(pixbuf)


It might be more effective to simply scale them before loading. I especially think so since I use these functions to load in 96x96 thumbnails from sometimes very large JPEGs, still very fast.

gtk.gdk.pixbuf_new_from_file_at_scale(..)gtk.gdk.pixbuf_new_from_file_at_size(..)


Scale image from URL. ( scale reference )

import pygtkpygtk.require('2.0')import gtkimport urllib2class MainWin:    def destroy(self, widget, data=None):        print "destroy signal occurred"        gtk.main_quit()    def __init__(self):        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)        self.window.connect("destroy", self.destroy)        self.window.set_border_width(10)        self.image=gtk.Image()        self.response=urllib2.urlopen(            'http://192.168.1.11/video/1024x768.jpeg')        self.loader=gtk.gdk.PixbufLoader()                 self.loader.set_size(200, 100)           #### works but throwing: glib.GError: Unrecognized image file format               self.loader.write(self.response.read())        self.loader.close()        self.image.set_from_pixbuf(self.loader.get_pixbuf())        self.window.add(self.image)        self.image.show()        self.window.show()    def main(self):        gtk.main()if __name__ == "__main__":    MainWin().main()

*EDIT: (work out fix) *

try:  self.loader=gtk.gdk.PixbufLoader()           self.loader.set_size(200, 100)               # ignore tihs:             #  glib.GError: Unrecognized image file format         self.loader.write(self.response.read())  self.loader.close()  self.image.set_from_pixbuf(self.loader.get_pixbuf())except Exception, err:  print err  pass