how to get local image input from user in python?

Viewed 43

I am building an auto image background remover website using remove.bg API I have the following code:

# Requires "requests" to be installed (see python-requests.org)
import requests

response = requests.post(
    'https://api.remove.bg/v1.0/removebg',
    files={'image_file': open('/path/to/file.jpg', 'rb')},
    data={'size': 'auto'},
    headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'},
)
if response.status_code == requests.codes.ok:
    with open('no-bg.png', 'wb') as out:
        out.write(response.content)
else:
    print("Error:", response.status_code, response.text) 

here on the 5th line, they have added the image path manually so what I should write in this code to get local image input from users just like they access ours??

1 Answers

Try an input statement:

# Requires "requests" to be installed (see python-requests.org)
import requests

file_path = input("Please insert the path to the image: ")

response = requests.post(
    'https://api.remove.bg/v1.0/removebg',
    files={'image_file': open(f'{file_path}', 'rb')},
    data={'size': 'auto'},
    headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'},
)
if response.status_code == requests.codes.ok:
    with open('no-bg.png', 'wb') as out:
        out.write(response.content)
else:
    print("Error:", response.status_code, response.text) 
Related