Skip to content Skip to sidebar Skip to footer

Scale An Image In Gtk

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?

Solution 1:

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

importgtkpixbuf= 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)

Solution 2:

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

Solution 3:

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 GtkWindowthis._image = new GtkImage();          
let pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filePath,width,height,true);
this._image.set_from_pixbuf(pixbuf);

Solution 4:

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

Solution 5:

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

Post a Comment for "Scale An Image In Gtk"