How to save <ipython.core.display.image object>
10,128
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.
Related videos on Youtube
Author by
M_D
Updated on June 04, 2022Comments
-
M_D almost 2 years
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:
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.
-
M_D over 7 yearsThanks! How do I write in a loop? for ex I want 3 pictures of (51.0, 0.0), (60.2, 5.2), (71.9, 8.9) in 1 png file
-
MattDMo over 7 yearsYou won't be able to save three images to a single PNG file without using something like Pillow to create a single canvas and place them on it first.
-
M_D over 7 yearsThanks for your guidance, but shoudnt it be with open("place_{0}.png".format(position), "wb") as png
-
MattDMo over 7 years@M_D nope. If you look at the
str.format()
format string syntax you'll see that{}
braces in the format string are substituted implicitly as long as the number of arguments match the number of paired braces. You can add explicit place marks if you want, especially if the arguments will be repeated or are out of order, but they're not necessary. -
M_D over 7 yearswhat i mean is in your answer you forget .format(position). We can use both "place_{0}.png".format(position) or "place_{}.png".format(position). But i got it to what i want, so I really appreciate your help!
-
suvayu over 5 yearsWhen writing to file, shouldn't
GoogleMap.image
beGoogleMap.image.data
?