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. So you shouldn't try to decode it like it was text.
Just do with open("image.png", "w") as file: file.write(data)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.