omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Saving an image file downloaded from a url

    Pythonista
    urllib image save
    2
    3
    2404
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • rhythmart
      rhythmart last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by 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)
        
        1 Reply Last reply Reply Quote 0
        • rhythmart
          rhythmart last edited by

          Many thanks.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors