社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何使用remove.bg在Python中删除照片的背景

shy zhan • 6 年前 • 2314 次点击  

我想用 https://www.remove.bg 删除我照片的背景。 这是我的代码:

import requests            
response = requests.post(
    'https://api.remove.bg/v1.0/removebg',
    files={'image_file': open('U:/Training/python/remove-bg-master/child.jpg', 'rb')},   
    data={'size': 'auto'},
    headers={'X-Api-Key': 'API key'}   
)
if response.status_code == requests.codes.ok:
    with open('child-no-bg.png', 'U:\Training\python\remove-bg-master') as out:           
        out.write(response.content)
else:
    print("Error:", response.status_code, response.text)

但我犯了个错误:

SSLError: HTTPSConnectionPool(host='api.remove.bg', port=443): Max retries exceeded with url: /v1.0/removebg (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')])")))

你能告诉我这里发生了什么事吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49793
文章 [ 2 ]  |  最新文章 6 年前
shy zhan
Reply   •   1 楼
shy zhan    6 年前

非常感谢你,格里斯马尔! 一。谢谢你的提醒。我删掉了。 2。我试过了 https://www.remove.bg 从Python中,它可以工作。 三。请求包是最新版本。 四。抓到了!我改正了。 5个。这个很好。

Grismar
Reply   •   2 楼
Grismar    6 年前

有几件事:

  • 你的问题中包含了你的API密钥;虽然这有助于我尝试你的问题,但是你可能不应该这样做,而是告诉人们在哪里可以得到他们自己的API密钥。
  • 如果你安装了像Postman这样的客户端 https://www.getpostman.com/downloads/ ,您可以在Python之外尝试请求,这正是我所做的,请求可以工作,所以问题不在于服务或API密钥。
  • 我试过你的代码,它对我很有用,最近安装了 requests ,请尝试更新您端的库,它可能会解决此问题。我用过 certifi==2019.9.11 requests==2.22.0 .
  • 你的台词 open('child-no-bg.png', 'U:\Training\python\remove-bg-master') 是错误的,假设您要在该目录中写入文件,请尝试以下操作 open(r'U:\Training\python\remove-bg-master\child-no-bg.png', 'wb') (这不是您的问题,但绝对是一个问题;第二个参数指示打开输出文件的模式,在这种情况下用于写入字节)
  • 我不知道该服务的详细信息,但如果您发送一个.jpg,它似乎不太可能返回一个.png的数据-您可能还想尝试以.jpg的形式编写文件;否则,您需要添加代码来转换格式。

给定错误消息,Python似乎无法验证SSL证书-该服务是否有可以首先尝试的http端点?

如果没有,请尝试使用SSL库通过https加载其他页面(任何页面,真的)-这些页面成功了吗,或者您可能发现了一般问题是什么?

您的问题可能与此有关: SSL3_GET_SERVER_CERTIFICATE certificate verify failed on Python when requesting (only) *.google.com