2009-08-12 124 views
13

GTKでは、どのように画像を拡大縮小できますか?今はPILで画像をロードし、あらかじめスケーリングしていますが、GTKでそれを行う方法はありますか?GTKで画像の拡大/縮小

答えて

15

そのためgtk.gdk.Pixbufを使用して、ファイルからイメージをロード:

import gtk 
pixbuf = gtk.gdk.pixbuf_new_from_file('/path/to/the/image.png') 

は、それをスケーリング:

pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR) 

その後、あなたはgtk.Imageでそれを使用したい場合は、ウィジェットを作成し、pixbufからイメージを設定します。

image = gkt.Image() 
image.set_from_pixbuf(pixbuf) 

それとも直接的な方法で:

image = gtk.image_new_from_pixbuf(pixbuf) 
+0

たちは、私は同じもののためではなく、CとGTK +の下で探しています....また、Cでこのソリューションを持つことができます.... GtkImageの使用* image = gtk_image_new_from_file() – User7723337

6

単にロードする前にそれらを拡張する方が効果的かもしれません。私は特に、これらの機能を使って96x96のサムネイルを時々非常に大きなJPEGからロードするので、とても速いと思います。

gtk.gdk.pixbuf_new_from_file_at_scale(..) 
gtk.gdk.pixbuf_new_from_file_at_size(..) 
1

URLのスケール画像。 (scale reference

import pygtk 
pygtk.require('2.0') 
import gtk 
import urllib2 

class MainWin: 

    def destroy(self, widget, data=None): 
     print "destroy signal occurred" 
     gtk.main_quit() 

    def __init__(self): 
     self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
     self.window.connect("destroy", self.destroy) 
     self.window.set_border_width(10) 
     self.image=gtk.Image() 

     self.response=urllib2.urlopen(
      'http://192.168.1.11/video/1024x768.jpeg') 

     self.loader=gtk.gdk.PixbufLoader()   
     self.loader.set_size(200, 100) 
     #### works but throwing: glib.GError: Unrecognized image file format  
     self.loader.write(self.response.read()) 
     self.loader.close() 
     self.image.set_from_pixbuf(self.loader.get_pixbuf()) 

     self.window.add(self.image) 
     self.image.show() 


     self.window.show() 

    def main(self): 
     gtk.main() 

if __name__ == "__main__": 
    MainWin().main() 

* EDIT:(修正をうまく)*

try: 
    self.loader=gtk.gdk.PixbufLoader()   
    self.loader.set_size(200, 100) 

      # ignore tihs: 
      # glib.GError: Unrecognized image file format  

    self.loader.write(self.response.read()) 
    self.loader.close() 
    self.image.set_from_pixbuf(self.loader.get_pixbuf()) 

except Exception, err: 
    print err 
    pass 
関連する問題