python - how to convert response to image with decoding? - Stack Overflow

admin2025-04-26  10

I was using this code on python previously

with open(f"image.jpg", "wb") as fh:
    fh.write(response.content)

and it was working fine, but now I changed lib for requests and the current script doesn't work

That is the new response.text example

while old response.text looks like:

tried this code to decode unicode:

with open(f"image.jpg", "wb") as fh:
    fh.write(response.text.encode().decode('unicode_escape').encode())

but the image is not correct, it does not display.

also tried: ee = base64.encodebytes(rim.content[1:-1].decode('unicode_escape').replace('\\n','').replace('\n','').replace('\\r','').replace('\r','').encode())

and as result got:

I did consider replacing all slashes and \n but still the result string is invalid. I believe there are some decode errors or my mistakes.

I use the same headers for content type and encoding in the request, so please, if it's possible, suggest solutions not related to the request itself.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745672212a313380.html

最新回复(0)