How to display an image from the Internet?

I wrote this simple script in python:

import gtk window = gtk.Window() window.set_size_request(800, 700) window.show() gtk.main() 

Now I want to download an image from the Internet (and not from my PC) into this window as follows:

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

How can i do this?

PS I do not want to upload an image! I just want to load an image from a URL.

+6
python gtk pygtk
source share
3 answers

Loads an image from a URL, but writes the data to gtk.gdk.Pixbuf instead of 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() 
+15
source share
  • Upload image. Google on how to upload files using python, there are easy-to-use libraries for this.

  • Upload the image to the widget. See how to display an image in GTK.

Sorry for the lack of details, but the answer will be long and you will still be better off reading these topics elsewhere.

Hope this helps!

+3
source share

Here is 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() 

Please note that this does load the image.

+1
source share

All Articles