Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  12] [ 0]  / answers: 1 / hits: 3535  / 2 Years ago, wed, december 8, 2021, 5:43:14

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


More From » python

 Answers
4

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


[#37348] Wednesday, December 8, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
taigysel

Total Points: 33
Total Questions: 136
Total Answers: 114

Location: Singapore
Member since Wed, Jan 13, 2021
3 Years ago
taigysel questions
;