Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save <ipython.core.display.image object>

I have png data that I can display via IPython.core.display.Image

Code example:

class GoogleMap(object):
    """Class that stores a PNG image"""
    def __init__(self, lat, long, satellite=True,
                    zoom=10, size=(400,400), sensor=False):
        """Define the map parameters"""
        base="http://maps.googleapis.com/maps/api/staticmap?"
        params=dict(
                sensor= str(sensor).lower(),
                zoom= zoom,
                size= "x".join(map(str, size)),
                center= ",".join(map(str, (lat, long) )),
                style="feature:all|element:labels|visibility:off"
                )

        if satellite:
            params["maptype"]="satellite"

        # Fetch our PNG image data
        self.image = requests.get(base, params=params).content
        
        
import IPython
IPython.core.display.Image(GoogleMap(51.0, 0.0).image)

Result:

result

How can I save this picture into a png file.

Im actually interested in putting this into a loop, so 1 png file has like 3 pictures continuously.

Thanks.

like image 294
M_D Avatar asked Oct 28 '25 05:10

M_D


1 Answers

All you need to do is use Python's standard file-writing behavior:

img = GoogleMap(51.0, 0.0)
with open("GoogleMap.png", "wb") as png:
    png.write(img.image)

Here's a very simple way of accessing the three lat/long pairs you want:

places = [GoogleMap(51.0, 0.0), GoogleMap(60.2, 5.2), GoogleMap(71.9, 8.9)]
for position, place in enumerate(places):
    with open("place_{}.png".format(position), "wb") as png:
        png.write(place.image)

I'll leave it up to you to write a function that takes arbitrary latitude/longitude pairs and saves images of them.

like image 194
MattDMo Avatar answered Oct 29 '25 18:10

MattDMo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!