Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display an image from web?

Tags:

python

pygtk

gtk

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.

like image 524
xRobot Avatar asked Oct 18 '10 18:10

xRobot


People also ask

How do I display an image on a website?

The HTML <img> tag is used to embed an image in a web page. Images are not technically inserted into a web page; images are linked to web pages. The <img> tag creates a holding space for the referenced image. The <img> tag is empty, it contains attributes only, and does not have a closing tag.

How do I display an image?

To display an image, use the <img> tag with the src attribute the way you'd use the href attribute in an <a> tag. It's important to provide the width and height of the image upfront to avoid layout issues and jumping visual effect.

How do I display an image as a link?

Complete HTML/CSS Course 2022 To use image as a link in HTML, use the <img> tag as well as the <a> tag with the href attribute. The <img> tag is for using an image in a web page and the <a> tag is for adding a link. Under the image tag src attribute, add the URL of the image. With that, also add the height and width.


3 Answers

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()
like image 158
unutbu Avatar answered Oct 08 '22 18:10

unutbu


  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!

like image 26
slezica Avatar answered Oct 08 '22 17:10

slezica


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.

like image 40
ptomato Avatar answered Oct 08 '22 18:10

ptomato