在 GTK 中缩放图像

发布于 2024-08-02 04:06:32 字数 65 浏览 3 评论 0原文

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

In GTK, how can I scale an image? Right now I load images with PIL and scale them beforehand, but is there a way to do it with GTK?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一人独醉 2024-08-09 04:06:32

为此,使用 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)

Load the image from a file using gtk.gdk.Pixbuf for that:

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

then scale it:

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

Then, if you want use it in a gtk.Image, crate the widget and set the image from the pixbuf.

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

Or maybe in a direct way:

image = gtk.image_new_from_pixbuf(pixbuf)
梦行七里 2024-08-09 04:06:32

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

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

It might be more effective to simply scale them before loading. I especially think so since I use these functions to load in 96x96 thumbnails from sometimes very large JPEGs, still very fast.

gtk.gdk.pixbuf_new_from_file_at_scale(..)
gtk.gdk.pixbuf_new_from_file_at_size(..)
情魔剑神 2024-08-09 04:06:32

从 URL 缩放图像。 ( 比例参考 )

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

Scale image from 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: (work out fix) *

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
骄兵必败 2024-08-09 04:06:32

任何人在 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);

anyone doing this in C. This is how it's done

//Assuming you already loaded the file and saved the filename
//GTK_IMAGE(image) is the container used to display the 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);
杀手六號 2024-08-09 04:06:32

仅供参考,这是一个根据窗口大小缩放图像的解决方案(这意味着您正在扩展 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);

Just FYI, here is a solution which scales the image based on window size (Implying you are implementing this in a class which extends 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);
甩你一脸翔 2024-08-09 04:06:32

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

actually when we use
gdk_pixbuf_scale_simple(pb,700,700,GDK_INTERP_BILINEAR); this function causes memory leakage (If we monitor task manager the memory requirement goes on increasing till it kills the process) when used with a timer event. How to solve that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文