python - 如何保存<ipython.core.display.image对象>

标签 python ipython display

我有可以通过 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)

结果:

result

如何将这张图片保存为 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/

相关文章:

Python newspace 持续存在

ipython - 有什么方法可以在编辑模式、Jupyter Notebook 中交换 Enter 和 Shift-Enter 输入命令?

python - 我想按住按钮持续移动,但即使我不抬起按键,KEYUP 的读数也是 true

python - 在没有分类器的情况下使用 scikit-learn 绘制混淆矩阵

python - 已安装包但未导入包

python - 在 IPython 中执行 .py 文件

python - ipyparallel 异常 : "You have attempted to connect to an IPython Cluster but no Controller could be found"

PHP - 如何使用 id 显示当前用户详细信息?

javascript - style.display ="block"仅针对默认div 在页面打开时不起作用

JavaScript - 循环遍历一次仅显示一个的非重复值