python 在 GTK 中缩放图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1269320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scale an image in GTK
提问by Claudiu
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?
在 GTK 中,如何缩放图像?现在我用 PIL 加载图像并预先缩放它们,但是有没有办法用 GTK 来做到这一点?
回答by markuz
Load the image from a file using gtk.gdk.Pixbuf for that:
使用 gtk.gdk.Pixbuf 从文件加载图像:
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.
然后,如果您想在 gtk.Image 中使用它,请创建小部件并从 pixbuf 设置图像。
image = gtk.Image()
image.set_from_pixbuf(pixbuf)
Or maybe in a direct way:
或者也许以直接的方式:
image = gtk.image_new_from_pixbuf(pixbuf)
回答by u0b34a0f6ae
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.
在加载之前简单地缩放它们可能更有效。我特别认为是因为我使用这些函数从有时非常大的 JPEG 加载 96x96 缩略图,但仍然非常快。
gtk.gdk.pixbuf_new_from_file_at_scale(..)
gtk.gdk.pixbuf_new_from_file_at_size(..)
回答by u0b34a0f6ae
Scale image from URL. ( scale reference)
从 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()
*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
回答by Bastian
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
).
仅供参考,这是一个根据窗口大小缩放图像的解决方案(暗示您正在一个扩展的类中实现它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);