0

I'm trying to use https://www.remove.bg to remove the background of my photos. Here is my code:

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)

But I got an error:

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')])")))

Could you please tell me what's going on here?

Mig82
  • 4,856
  • 4
  • 40
  • 63
shy zhan
  • 35
  • 6

2 Answers2

0

A few things:

  • You've included your API key in your question; although that helped me try your problem, you probably shouldn't do that and instead tell people where they can get their own.
  • If you install a client like Postman https://www.getpostman.com/downloads/, you can try the request outside of Python, which is what I did and the request works, so it's not the service or the API key that is the problem.
  • I tried your code and it works for me, with a recent install of requests, please try updating the library on your end, it may fix the issue. I used certifi==2019.9.11 and requests==2.22.0.
  • Your line open('child-no-bg.png', 'U:\Training\python\remove-bg-master') is wrong, assuming you want to write the file in that directory, try something like open(r'U:\Training\python\remove-bg-master\child-no-bg.png', 'wb') (this is not your problem, but definitely a problem; the second parameter indicates the mode in which the output file is to be opened, for writing bytes in this case)
  • I don't know the details of the service, but it seems unlikely that if you send it a .jpg, it will return the data for a .png - you may want to try writing the file as a .jpg as well; otherwise, you'll need to add code to convert the format.

Given the error message, it appears that Python is having trouble verifying the SSL certificate - does the service have an http endpoint you could try first?

If not, try loading other pages over https using the SSL library (any page, really) - do those succeed or can you perhaps find out what the general problem is?

Your issue may be related to this: SSL3_GET_SERVER_CERTIFICATE certificate verify failed on Python when requesting (only) *.google.com

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Thank you so much, Grismar! 1. Thanks for your remind. I deleted it. 2. I tried https://www.remove.bg out of Python, it works. 3. Package of requests is the latest version. – shy zhan Oct 08 '19 at 16:33
  • But you still have the same problem, even with an up to date `certifi` and `requests`? Are you running the script on the same machine as the test? Does it allow connections on port 443 (https)? Another option would be to try adding `verify=False` to your `requests.post()` call, but be careful with this - it opens you up to man in the middle attacks for anything serious. – Grismar Oct 08 '19 at 21:59
0

Thank you so much, Grismar! 1. Thanks for your remind. I deleted it. 2. I tried https://www.remove.bg out of Python, it works. 3. The package of requests is the latest version. 4. Good catch! I corrected it. 5. This one is fine.

shy zhan
  • 35
  • 6