Possible Duplicate:
How do I copy a remote image in python?
I'm using the Python httplib2 library to retrieve a .png image from the web.
E.g.
import httplib2
h = httplib2.Http('.cache')
response, content = h.request('http://www.example.com/image.png')
My question is, what are some good ways to go about saving this image to the local file system? At the moment I've tried using PIL with the following as an example:
import httplib2
import StringIO
from PIL import Image
h = httplib2.Http('.cache')
response, content = h.request('http://www.google.com.au/images/nav_logo40.png')
if response.status != 200:
print "http request failed."
im = Image.open(StringIO.StringIO(content))
try:
im.verify()
im.save('some_image.png')
except Exception:
print "save failed."
With this code, everything runs fine until the saving happens with im.save()
and an exception is thrown. Can someone point out what I'm doing wrong and a correct alternative?