简体   繁体   English

将str(从response.content获取)保存为图像时出错

[英]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看起来像这样(缩短的示例):

eyJpZCI6IndzX2Y5YnVqaWVwamVzMnRyNWpjM2RpXzE1MTY2MTM5MjgzMzMi‌​LC.............YTZPZ‌​UVjWkwva0FBQUFBRWxGV‌​GtTdVFtQ0MifQ==

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.

相关问题 检查“response.content”中的字符串,引发“TypeError: a bytes-like object is required, not &#39;str&#39;” - Check for string in “response.content” raising “TypeError: a bytes-like object is required, not 'str'” 如何打印response.content的特定部分 - How to print a specific part of response.content self.assertTrue(response.content中的post.text) - 断言错误 - self.assertTrue(post.text in response.content) – Assertion Error 如何从 response.content Python reuests.get 读取所有页面 - How to read all the pages from response.content Python reuests.get response.status_code为200,但response.content为空 - response.status_code is 200, but response.content is empty 如何使用 Python &#39;requests&#39; 库 &#39;response.content&#39; 获取 HTML 响应的字节表示 - How to get byte representation of HTML response like with the Python 'requests' library 'response.content' django.test.Client和response.content与streaming_content - django.test.Client and response.content vs. streaming_content 如何修改 Django 中间件 __call__ 方法中的 response.content - How can I modify the response.content in a Django Middleware __call__ method 将请求 response.content 放入队列后,multiprocessing.Process 不会终止 - multiprocessing.Process doesn't terminate after putting requests response.content to queue 为什么response.content可以读取两次而不能解码为json - Why can response.content be read twice and can't be decoded to json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM