【问题标题】:Scale an image in GTK在 GTK 中缩放图像
【发布时间】:2009-08-12 23:44:38
【问题描述】:

在 GTK 中,如何缩放图像?现在我使用 PIL 加载图像并预先缩放它们,但是有没有办法使用 GTK 来做到这一点?

【问题讨论】:

    标签: python user-interface image gtk pygtk


    【解决方案1】:

    为此使用 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 = gtk.Image()
    image.set_from_pixbuf(pixbuf)
    

    或者直接的方式:

    image = gtk.image_new_from_pixbuf(pixbuf)
    

    【讨论】:

    • 我们能否在 C 中也有这个解决方案....我正在寻找相同的东西,但在 C 和 GTK+ 下....使用 GtkImage *image = gtk_image_new_from_file()
    • 有一个错字:gkt.Image()
    【解决方案2】:

    在加载之前简单地缩放它们可能更有效。我特别这么认为,因为我使用这些函数从有时非常大的 JPEG 文件中加载 96x96 的缩略图,速度仍然非常快。

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

    【讨论】:

    • 这使用更少的内存并且速度更快。将其包装在 try 和 except 中,它应该是公认的答案。
    【解决方案3】:

    从 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()
    

    *编辑:(解决)*

    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
    

    【讨论】:

      【解决方案4】:

      仅供参考,这是一个根据窗口大小缩放图像的解决方案(暗示您在扩展 GtkWindow 的类中实现它)。

      let [width, height] = this.get_size(); // Get size of GtkWindow
      this._image = new GtkImage();          
      let pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filePath,width,height,true);
      this._image.set_from_pixbuf(pixbuf);
      

      【讨论】:

        【解决方案5】:

        任何人都在 C 中这样做。这就是它的完成方式

        //假设你已经加载了文件并保存了文件名 //GTK_IMAGE(image)是用来显示图片的容器

        GdkPixbuf *pb;
        
        pb = gdk_pixbuf_new_from_file(file_name, NULL);
        pb = gdk_pixbuf_scale_simple(pb,700,700,GDK_INTERP_BILINEAR);
                    gtk_image_set_from_pixbuf(GTK_IMAGE(image), pb);
        

        【讨论】:

          【解决方案6】:

          实际上当我们使用 gdk_pixbuf_scale_simple(pb,700,700,GDK_INTERP_BILINEAR);当与计时器事件一起使用时,此函数会导致内存泄漏(如果我们监视任务管理器,内存需求会继续增加直到它杀死进程)。如何解决这个问题

          【讨论】:

            猜你喜欢
            • 2013-05-29
            • 1970-01-01
            • 2020-06-18
            • 1970-01-01
            • 2019-12-11
            • 2011-01-16
            • 2019-03-04
            • 2014-08-12
            • 1970-01-01
            相关资源
            最近更新 更多