Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I trying to use the library gdk for scale down an image and apply it to a GdkImage.

This is the code pixbuf = Gdk.pixbuf_new_from_file(fileName) pixbuf = pixbuf.scale_simple(100, 100, Gdk.INTERP_BILINEAR)

The problem is that python can't find Gdk even if I use everything in lowercase

Error: pixbuf = Gdk.pixbuf_new_from_file(fileName) NameError: global name 'Gdk' is not defined

I don't know what should I do because I tried to import Gdk but nothing is changed

share|improve this question
up vote 9 down vote accepted

Try importing it like this:

 from gi.repository import Gtk
 from gi.repository.GdkPixbuf import Pixbuf, InterpType


 pixbuf = Pixbuf.new_from_file(filename)
 pixbuf = pixbuf.scale_simple(100, 100, InterpType.BILINEAR)

I would recommend using the command below because it automatically scales it when it reads it in. Just specify how big (pixels) you want the image to be:

 pixbuf = Pixbuf.new_from_file_at_size(size_x, size_y, filename)
  • Using scale_simple() does not preserve aspect ratio.
  • Using new_from_file_at_size() preserves aspect ratio
share|improve this answer
Thanks, I will try it later – max246 Jun 26 '12 at 10:34
Yes it works :) – max246 Jul 1 '12 at 11:34

I had the same problem. Your answer almost worked for me, but I got the following errormessage:

AttributeError: 'gi.repository.Gdk' object has no attribute 'INTERP_BILINEAR'

But I found the definition of it here gtkmm:gdkmm Enums and Flags:

Gdk::InterpType { 

So for me it worked when I called the function like this:

from gi.repository import Gtk, Gdk
from gi.repository.GdkPixbuf import Pixbuf
pixbuf = Pixbuf.new_from_file('mypic.gif')
pixbuf = pixbuf.scale_simple(desired_width, desired_height, 2) # 2 := BILINEAR

...maybe this helps someone with the same problem ;)

share|improve this answer
Why not edit the answer and solve the problem? – Braiam Nov 30 '13 at 18:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.