[英]error when saving the str(getting from response.content) as an image
i am using the requests in python to hit an api and get a response: 我正在使用python中的请求击中api并获得响应:
response = requests.request("POST", url, data=payload, headers=headers)
i have image data in response.content 我有图像数据response.content
print type(response.content)
gives: 给出:
<type 'str'>
now i have to save this data as an image file on my local : i am using ubuntu 16.04 and python 2.7 现在我必须将此数据另存为本地文件上的图像文件:我正在使用ubuntu 16.04和python 2.7
i am trying this: 我正在尝试:
image=response.content
result=open('img.png','wb')
result.write(image)
but when trying to open the resulting file, this error is shown: 但是当尝试打开生成的文件时,显示此错误:
Fatal error reading PNG image file: Not a PNG file
response.content
looks like this (shortened sample): response.content
看起来像这样(缩短的示例):
eyJpZCI6IndzX2Y5YnVqaWVwamVzMnRyNWpjM2RpXzE1MTY2MTM5MjgzMzMiLC.............YTZPZUVjWkwva0FBQUFBRWxGVGtTdVFtQ0MifQ==
i have also tried encoding response.content to base64 encoding and then tried writing my image without success. 我也尝试将response.content编码为base64编码,然后尝试写入我的图像而没有成功。 what should i do? 我该怎么办?
Have you tried like this : 您是否尝试过像这样:
image=response.content
fh = open("imageToSave.png", "wb")
fh.write(base64.decodebytes(image))
fh.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.