![](/img/trans.png)
[英](django) 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
[英]UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte. How to fix it
當我運行此代碼時出現錯誤:
print(data.decode("utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
我的代碼:
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"))
如何解決?
我也嘗試這樣做,但沒有幫助。 但是錯誤已經改變了。
如果我更換這個
print(data.decode("utf-8"))
有了這個
print(data)
我有這個
根據他們的API 文檔,響應內容是結果圖像。 所以你不應該像文本一樣嘗試解碼它。
只需with open("image.png", "w") as file: file.write(data)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.