简体   繁体   English

UnicodeDecodeError:“utf-8”编解码器无法解码 position 中的字节 0x89 0:起始字节无效。 如何修复

[英]UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte. How to fix it

When I run this code I get an error:当我运行此代码时出现错误:

    print(data.decode("utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

My code:我的代码:

import http.client
import json
from dotenv import load_dotenv
import os

load_dotenv()

conn = http.client.HTTPSConnection("api.remove.bg")
apikey = os.getenv('apikey')
imageurl = 'https://www.remove.bg/example-hd.jpg'
payload = json.dumps({
  "image_file_b64": "",
  "image_url": imageurl,
  "size": "preview",
  "type": "auto",
  "type_level": "1",
  "format": "auto",
  "roi": "0% 0% 100% 100%",
  "crop": False,
  "crop_margin": "0",
  "scale": "original",
  "position": "original",
  "channels": "rgba",
  "add_shadow": False,
  "semitransparency": True,
  "bg_color": "",
  "bg_image_url": ""
})
headers = {
  'X-API-Key': apikey,
  'Content-Type': 'application/json'
}
conn.request("POST", "/v1.0/removebg", payload, headers)
res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

How to fix it?如何解决?

I also tried doing this, but it didn't help.我也尝试这样做,但没有帮助。 But the error has changed.但是错误已经改变了。

if i replace this如果我更换这个

print(data.decode("utf-8"))

with this有了这个

print(data)

i have this我有这个

According to their API documentation , the response content is the result image.根据他们的API 文档,响应内容是结果图像。 So you shouldn't try to decode it like it was text.所以你不应该像文本一样尝试解码它。
Just do with open("image.png", "w") as file: file.write(data) .只需with open("image.png", "w") as file: file.write(data)

暂无
暂无

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

相关问题 (django) 'utf-8' 编解码器无法解码位置 0 的字节 0x89:无效的起始字节 - (django) 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte “utf-8”编解码器无法解码位置 0 中的字节 0x89:起始字节无效 - 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte 在 Django 中上传图像:“utf-8”编解码器无法解码位置 246 中的字节 0x89:起始字节无效 - Uploading Image in Django: 'utf-8' codec can't decode byte 0x89 in position 246: invalid start byte “utf-8”编解码器无法解码字节 0x89 - 'utf-8' codec can't decode byte 0x89 如何修复:UnicodeDecodeError:“utf-8”编解码器无法解码 position 中的字节 0x81 18:起始字节无效 - How to fix: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 18: invalid start byte UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 3131 中的字节 0x80:起始字节无效 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte Python UnicodeDecodeError:“ utf-8”编解码器无法解码位置2的字节0x8c:无效的起始字节 - Python UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8c in position 2: invalid start byte UnicodeDecodeError:'utf-8'编解码器无法解码位置3的字节0x97:无效的起始字节 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x97 in position 3: invalid start byte `UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte` - `UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte` UnicodeDecodeError:“utf-8”编解码器无法解码 position 76 中的字节 0x81:起始字节无效 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 76: invalid start byte
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM