1

我在 Jupyter Notebook 中有一个 Azure Maps API 调用,它返回 .png 格式的地图图块。该调用效果很好,但我不知道如何将其显示为图像而不是二进制文本。

- API 调用:

import requests
from ipywidgets import Image

url = "https://atlas.microsoft.com/map/static/png"

querystring = {
    "api-version":"1.0",
    "subscription-key":"<myRedactedAPIkey>",
    "layer":"basic",
    "zoom":"5",
    "center":"-122.3950336,47.566848",
    "height":"600",
    "width":"600",
    "pins":"default||-121.95066667 45.9135|-121.062 46.707",
    "format":"png",
    "path":"ra300||-122.3950336 47.566848"
}

payload = ""
headers = {
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, data=payload, headers=headers, params=querystring)

print(response.text)

结果是:

�PNG

IHDRX�f��sRGB���gAMA���a    pHYs���o�d��IDATx^��wt,K~�  �2ÕYqGg4+�iGsVgGg5Z�ќ]IT�Rs9\J䈤4r$r%�a���nv������}��wͻ������{[�By�20U\�6��@T"
��
�A�E��ֵ���|�%۶��O�N�#���dX��F��Y�p����y�l3�T�8;�Y�p�O҉#�վ8���yf����+5.����@0���q���Jތ�k��(�5�О���u���gBl�=�E���@�J����m=f�k&h��^��Z��Ms��̊\�J���if��C��:2_ <etc.>

想:

在此处输入图像描述

有什么建议吗?谢谢你。

EDIT2:这是有效的查询。谢谢大家的帮助。

import requests
from IPython.display import Image, display

url = "https://atlas.microsoft.com/map/static/png"
payload = ""
querystring = {
        "api-version":"1.0",
        "subscription-key":"<myApiKeyRedacted>",
        "format":"png",
        "layer":"basic",
        "zoom":"5",
        "center":"-122.3950336,47.566848",
        "height":"600",
        "width":"600",
        "pins":"default||-121.95066667 45.9135|-121.062 46.707",
        "path":"ra300000||-122.3950336 47.566848"
    }
headers = {
    'cache-control': "no-cache"
    }

r = requests.get(url,data=payload, headers=headers, params=querystring, stream=all)

display(Image(r.content))
4

2 回答 2

3

以下是最终的工作:出现 , 的组合stream=allIpython.display成功.content了。

import requests
from IPython.display import Image, display

url = "https://atlas.microsoft.com/map/static/png"
payload = ""
querystring = {
        "api-version":"1.0",
        "subscription-key":"<myApiKeyRedacted>",
        "format":"png",
        "layer":"basic",
        "zoom":"5",
        "center":"-122.3950336,47.566848",
        "height":"600",
        "width":"600",
        "pins":"default||-121.95066667 45.9135|-121.062 46.707",
        "path":"ra300000||-122.3950336 47.566848"
    }
headers = {
    'cache-control': "no-cache"
    }

r = requests.get(url,data=payload, headers=headers, params=querystring, stream=all)

display(Image(r.content))
于 2019-04-19T13:14:38.657 回答
0

由于response.text似乎是一个有效的 PNG 图像并且您正在使用 ipywidgets Image,您是否尝试使用它?

widgets.Image(
    value=response.text,
    format='png',
    width=300,
    height=400,
)
于 2019-04-16T08:05:59.443 回答