将urllib中的字节转换为光栅对象

2024-05-29 08:27:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图检索SMAP图像的单个观测值,用于模型计算。该模型运行良好,我只需要弄清楚如何将响应从urllib(以字节为单位)转换为光栅对象

我不熟悉如何在Python中使用字节,因此我不确定接下来要采取什么步骤,或者这是否可能

from http.cookiejar import CookieJar
import urllib.parse
import urllib.request

#User Inputs
username = "username"
password = "password"
day = "2019.07.12" #Must be input in YYYY.MM.DD Format

通过输入,我正在查询Earthdata

url = "https://n5eil01u.ecs.nsidc.org/DP6/SMAP/SPL4SMAU.004/" + day + "/SMAP_L4_SM_aup_20190712T120000_Vv4030_001.h5"
password_manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, "https://urs.earthdata.nasa.gov", username, password)
cookie_jar = CookieJar()
opener = urllib.request.build_opener(
    urllib.request.HTTPBasicAuthHandler(password_manager),
    urllib.request.HTTPHandler(debuglevel=1),    # Uncomment these two lines to see
    urllib.request.HTTPSHandler(debuglevel=1),   # details of the requests/responses
    urllib.request.HTTPCookieProcessor(cookie_jar))
urllib.request.install_opener(opener)

request = urllib.request.Request(url)
response = urllib.request.urlopen(request)

这部分代码工作正常,响应是http.client.HTTPResponse对象。然后我读到:

SMAP = response.read()

使用.read(),它将成为一个字节对象

我需要弄清楚如何使用我作为光栅对象绘制的图像,有什么想法吗

非常感谢你的帮助


Tags: 对象模型图像importhttp字节request光栅

热门问题