Again, I believe there is more than one way to do it. What follows is how I solved it.
There is a nice built-in library in Python called urllib which we will use to download the image in question.
The code is simple:
import urllib
def getimagefromweb(url):
'''Downloads content from given url and saves it as image.'''
try:
u = urllib.urlopen(url) # open url
content = u.read() # read the openned url
u.close() # url was closed
except IOError:
print('IOError')
except:
print('Unknown url error')
# saving what was downloaded
f = open('img.jpg','wb')
f.write(content)
f.close() # file was closed
myurl = 'http://siemens.mesto-zatec.cz/obrazek.jpg'
getimagefromweb(myurl)
print 'all OK'
The (only) function "getimagefromweb" receives a url with content to be downloaded and then reads from this adress in the same way that is used when reading/writing files. When dealing with web it is very advisable to use error-catching code like this one. It is common that the website will not respond in time and without error catching your application would crash.
As a final task the function saves image to harddisk so that it can be later re-read and shown on the desktop. This is a workaroud because I could not figure out a way how to display it directly.
The url leads to the webcam of the main square of the Zatec city where I was born and have grown up. It has quite interesting medieval centre so its nice to have it shown on desktop :-)
1 comment:
Lucky Club Casino Site – Live Casino Games for Live players
Lucky Club Casino. The number 1 online gambling destination for players in the UK for live casino. In luckyclub.live our exciting new casino you can enjoy over 400
Post a Comment