我有可以通过 IPython.core.display.Image 显示的 png 数据
代码示例:
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)
结果:
如何将这张图片保存为 png 文件。
我实际上有兴趣将其放入循环中,因此 1 个 png 文件连续有 3 张图片。
谢谢。
最佳答案
您所需要做的就是使用 Python 的标准文件写入行为:
img = GoogleMap(51.0, 0.0)
with open("GoogleMap.png", "wb") as png:
png.write(img.image)
这是访问您想要的三个纬度/经度对的非常简单的方法:
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)
我将让您编写一个函数来获取任意纬度/经度对并保存它们的图像。
关于python - 如何保存<ipython.core.display.image对象>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41512842/