tags:

views:

52

answers:

3

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.

+3  A: 
  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!

Santiago Lezica
I'd use GTK to download it rather than Python, but otherwise yes.
Ignacio Vazquez-Abrams
I don't want download the image ! I just want load the image from the URL :)
xRobot
@xRobot: What precisely did you think loading the image from a URL involved?
Ignacio Vazquez-Abrams
@xRobot: hint -- starts with "d" and ends with "ownloading it" :D
Santiago Lezica
I can't load the image in the window without downloading it ?
xRobot
I think he might be referring to hotlinking a picture on the web?
jlv
yes I want hotlinking the image without download it if possible :D
xRobot
If you want to actually DISPLAY the image ON YOUR computer, you HAVE to download it. Think of it this way: an URL is a name pointing to a thing. If you want to show that thing, you need to get the data behind that name. This is what web browsers do, and what you must do to display the image. Or, you can display the link to it, which is just the name.
Santiago Lezica
... ok ok sorry :D
xRobot
Haha no problem, man! Sorry for the humorous answers above
Santiago Lezica
+1  A: 

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.

ptomato
It also requires pywebkitgtk, which is not a small requirement.
Ignacio Vazquez-Abrams
I suppose you could also use libsoup, but I think this fits the OP's needs best ;-)
ptomato
+3  A: 

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