39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
import requests
|
|
|
|
# Define the API endpoint and parameters
|
|
api_url = "https://api.removal.ai/3.0/remove"
|
|
api_key = "93D96377-ED5E-7CC1-CD9D-05017285C46A"
|
|
|
|
# Define the file path to the image
|
|
image_path = "photo.png"
|
|
|
|
headers = {
|
|
"Rm-Token": api_key
|
|
}
|
|
files = {
|
|
"image_file": open(image_path, "rb")
|
|
}
|
|
data = {
|
|
"get_file": "1" # Ensures the processed file is returned
|
|
}
|
|
|
|
try:
|
|
# Make the POST request
|
|
response = requests.post(api_url, headers=headers, files=files, data=data)
|
|
|
|
# Save the output file if the request is successful
|
|
if response.status_code == 200:
|
|
with open("transparent_image.png", "wb") as output_file:
|
|
output_file.write(response.content)
|
|
print("Transparent image saved as 'transparent_image.png'")
|
|
else:
|
|
print(f"Error: Received status code {response.status_code}")
|
|
print("Response:", response.text)
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
print("An error occurred:", e)
|
|
finally:
|
|
# Close the file
|
|
files["image_file"].close()
|
|
|