Scale an image in GTK

18,141

Solution 1

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)

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(..)
Share:
18,141
Claudiu
Author by

Claudiu

Graduated from Brown University. E-mail: [email protected]

Updated on June 04, 2022

Comments

  • Claudiu
    Claudiu almost 2 years

    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?

  • User7723337
    User7723337 over 14 years
    Can 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()
  • Ikem Krueger
    Ikem Krueger about 6 years
    There is a typo: gkt.Image()
  • Eric Sebasta
    Eric Sebasta over 2 years
    This uses less memory and is faster. wrap it in a try and except and it should be the accepted answer.