How to download a image in a webpage in python

Hi..to all ..today i learnt how to download a image in a webpage in python…here the code is..

we need to copy the url of the image and give that in our code ..

#! usr/bin/python
import urllib2
import webbrowser
import os
# find yourself a picture on a web page you like
# (right click on the picture, look under properties and copy the address)
picture_page=”http://1.bp.blogspot.com/-_l6saamIXzk/T1hikYw-bUI/AAAAAAAAD7I/073fMnOIcwQ/s640/bhavana-stills-86-473×500.jpg”# give the url of the image to download
#webbrowser.open(picture_page) # test
# open the web page picture and read it into a variable
opener1 = urllib2.build_opener()
page1 = opener1.open(picture_page)
my_picture = page1.read()
# open file for binary write and save picture
# picture_page[-4:] extracts extension eg. .gif
# (most image file extensions have three letters, otherwise modify)
filename = “my_image” + picture_page[-4:]
print filename # test
fout = open(filename, “wb”)
fout.write(my_picture)
fout.close()
# was it saved correctly?
# test it out …
webbrowser.open(filename)
# or …
# on Windows this will display the image in the default viewer
#os.startfile(filename)

Run the file by Python filename.py

output:

Thats it…Thanks…..:)

Author: Balaji

Hi..My name is Balaji and i am working as a Senior Software Developer in India. I am interested in Shell scripts, python, erlang , linux kernel .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: