Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

2 Answers 2

up vote 8 down vote accepted

Try importing it like this:

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

then:

 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 { 
  Gdk::INTERP_NEAREST, 
  Gdk::INTERP_TILES, 
  Gdk::INTERP_BILINEAR, 
  Gdk::INTERP_HYPER 
}

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

 
discard

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.