In GTK, how can I scale an image? Right now I load images with PIL and scale them beforehand, but is there a way to do it with GTK?
6 Answers
Load the image from a file using gtk.gdk.Pixbuf for that:
import gtk
pixbuf = 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)
- 
                    2Can we have this solution in C also.... i am looking for same stuff but under C and GTK+.... Using GtkImage *image = gtk_image_new_from_file() – User7723337 Nov 11 '09 at 11:30
- 
                    There is a typo: _gkt.Image()_ – Ikem Krueger Apr 16 '18 at 14:18
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(..)
 
    
    - 48,117
- 14
- 92
- 101
- 
                    This uses less memory and is faster. wrap it in a try and except and it should be the accepted answer. – Eric Sebasta Aug 15 '21 at 17:58
Scale image from URL. ( scale reference )
import pygtk
pygtk.require('2.0')
import gtk
import urllib2
class 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
anyone doing this in C. This is how it's done
//Assuming you already loaded the file and saved the filename //GTK_IMAGE(image) is the container used to display the image
GdkPixbuf *pb;
pb = gdk_pixbuf_new_from_file(file_name, NULL);
pb = gdk_pixbuf_scale_simple(pb,700,700,GDK_INTERP_BILINEAR);
            gtk_image_set_from_pixbuf(GTK_IMAGE(image), pb);
 
    
    - 546
- 5
- 14
Just FYI, here is a solution which scales the image based on window size (Implying you are implementing this in a class which extends GtkWindow).
let [width, height] = this.get_size(); // Get size of GtkWindow
this._image = new GtkImage();          
let pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filePath,width,height,true);
this._image.set_from_pixbuf(pixbuf);
 
    
    - 620
- 5
- 14
actually when we use gdk_pixbuf_scale_simple(pb,700,700,GDK_INTERP_BILINEAR); this function causes memory leakage (If we monitor task manager the memory requirement goes on increasing till it kills the process) when used with a timer event. How to solve that
 
     
     
    