如何显示来自网络的图像?

发布于 2024-09-28 00:22:17 字数 425 浏览 8 评论 0原文

我用 python 编写了这个简单的脚本:

import gtk

window = gtk.Window()
window.set_size_request(800, 700)

window.show()
gtk.main()

现在我想在这个窗口中加载来自网络(而不是我的电脑)的图像,如下所示:

http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg

我该怎么做?

PS我不想下载图片!我只想从 URL 加载图像。

I have written this simple script in python:

import gtk

window = gtk.Window()
window.set_size_request(800, 700)

window.show()
gtk.main()

now I want to load in this window an image from web ( and not from my PC ) like this:

http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg

How can I do that ?

P.S. I don't want download the image ! I just want load the image from the URL.

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

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

发布评论

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

评论(3

苦笑流年记忆 2024-10-05 00:22:17

这会从 url 下载图像,但将数据写入 gtk.gdk.Pixbuf 而不是文件:

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()
        response=urllib2.urlopen(
            'http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg')
        loader=gtk.gdk.PixbufLoader()
        loader.write(response.read())
        loader.close()        
        self.image.set_from_pixbuf(loader.get_pixbuf())
        # This does the same thing, but by saving to a file
        # fname='/tmp/planet_x.jpg'
        # with open(fname,'w') as f:
        #     f.write(response.read())
        # self.image.set_from_file(fname)
        self.window.add(self.image)
        self.image.show()
        self.window.show()

    def main(self):
        gtk.main()

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

This downloads the image from a url, but writes the data into a gtk.gdk.Pixbuf instead of to a file:

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()
        response=urllib2.urlopen(
            'http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg')
        loader=gtk.gdk.PixbufLoader()
        loader.write(response.read())
        loader.close()        
        self.image.set_from_pixbuf(loader.get_pixbuf())
        # This does the same thing, but by saving to a file
        # fname='/tmp/planet_x.jpg'
        # with open(fname,'w') as f:
        #     f.write(response.read())
        # self.image.set_from_file(fname)
        self.window.add(self.image)
        self.image.show()
        self.window.show()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    MainWin().main()
寻梦旅人 2024-10-05 00:22:17
  1. 下载图像。 Google 关于如何使用 python 下载文件,有易于使用的库。

  2. 将图像加载到小部件中。了解如何在 GTK 中显示图像。

很抱歉缺乏细节,但答案会很长,你最好还是在其他地方阅读这些主题。

希望有帮助!

  1. Download the image. Google on how to download files with python, there are easy-to-use libraries for that.

  2. Load the image into a widget. Look up how to display an image in GTK.

Sorry for the lack of detail, but the answer would get long and you'd still be better off reading on those subjects somewhere else.

Hope it helps!

把梦留给海 2024-10-05 00:22:17

这是一个使用 WebKit 的简单脚本:

#!/usr/bin/env python
import gtk
import webkit

window = gtk.Window()
window.set_size_request(800, 700)
webview = webkit.WebView()
window.add(webview)
window.show_all()

webview.load_uri('http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg')

gtk.main()

不过请注意,这实际上会下载图像。

Here's a simple script using WebKit:

#!/usr/bin/env python
import gtk
import webkit

window = gtk.Window()
window.set_size_request(800, 700)
webview = webkit.WebView()
window.add(webview)
window.show_all()

webview.load_uri('http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg')

gtk.main()

Take note, though, that this does in fact download the image.

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