Forum Archive

Saving an image file downloaded from a url

rhythmart

I have downloaded a Google static map through their API using:

data = urllib.urlopen(my_url).read()
buffer = StringIO(data)
image = Image.open(buffer)

where "my_url" is configured to deliver the static map I want.

When I use image.show() the map shows exactly what I'd expect.

However, when I use image.save('google_static_map.png') Pynthonista crashes. If I change the save command to image.save('google_static_map', 'png') it doesn't crash but the file google_static_map is created with no .png extension and no content (it says No Script Loaded).

My image is a PIL.PngImagePlugin.PngImageFile with size 1200x800 and mode P.

Can you please help? Am I missing something obvious?

Thanks in advance.

omz

This is a bug in version 1.5. In your case, it's easy to work around it though: You already have the data in PNG format, so there's no reason to use the image's save method to encode it again – you can just write the downloaded data as-is:

data = urllib.urlopen(my_url).read()
with open('google_static_map.png', 'w') as f:
    f.write(data)
rhythmart

Many thanks.