简体   繁体   English

如何在Jupyter Notebook中显示来自API响应的图像?

[英]How to display image from API response in Jupyter Notebook?

I have an Azure Maps API call in a Jupyter Notebook that returns a map tile in .png format. 我在Jupyter Notebook中有一个Azure Maps API调用,该调用返回.png格式的地图图块。 The call works great, but I can't figure out how to display it as an image rather than as binary text. 该调用的效果很好,但我不知道如何将其显示为图像而不是二进制文本。

- API Call: -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)

Results in: 结果是:

�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.>

Want: 想:

在此处输入图片说明

Any advice? 有什么建议吗? Thank you. 谢谢。

EDIT2: Here is the query that works. EDIT2:这是有效的查询。 Thanks for your assistance everyone. 感谢大家的帮助。

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))

Here is what ended up working: Appears a combo of stream=all , Ipython.display and .content did the trick. 这就是最终的结果:出现了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))

Since response.text seems to be a valid PNG image and you are using ipywidgets Image , did you try to use this? 由于response.text似乎是有效的PNG图像,并且您使用的是ipywidgets Image ,您是否尝试使用它?

widgets.Image(
    value=response.text,
    format='png',
    width=300,
    height=400,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM