Simply copy and paste the code into your project, and update the placeholder with your own API key.
import http.client
import mimetypes
import os
import uuid
# Please replace with your own apiKey
apiKey = 'YOUR_API_KEY_HERE'
def remove_background(input_image_path, output_image_path):
# Define multipart boundary
boundary = '----------{}'.format(uuid.uuid4().hex)
# Get mimetype of image
content_type, _ = mimetypes.guess_type(input_image_path)
if content_type is None:
content_type = 'application/octet-stream' # Default type if guessing fails
# Prepare the POST data
with open(input_image_path, 'rb') as f:
image_data = f.read()
filename = os.path.basename(input_image_path)
body = (
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"image_file\"; filename=\"{filename}\"\r\n"
f"Content-Type: {content_type}\r\n\r\n"
).encode('utf-8') + image_data + f"\r\n--{boundary}--\r\n".encode('utf-8')
# Set up the HTTP connection and headers
conn = http.client.HTTPSConnection('sdk.photoroom.com')
headers = {
'Content-Type': f'multipart/form-data; boundary={boundary}',
'x-api-key': apiKey
}
# Make the POST request
conn.request('POST', '/v1/segment', body=body, headers=headers)
response = conn.getresponse()
# Handle the response
if response.status == 200:
response_data = response.read()
with open(output_image_path, 'wb') as out_f:
out_f.write(response_data)
print("Image saved to", output_image_path)
else:
print(f"Error: {response.status} - {response.reason}")
print(response.read())
# Close the connection
conn.close()
Step 3: Call the API
Now it's time to call the API in your code! Just use the function remove_background() and pass as arguments both the path of the original image and the path where the result image should be saved.
from remove_background import remove_background
# Example usage
input_path = "test.jpg"
output_path = "result.png"
remove_background(input_path, output_path)
You're done!
That's it! With just these three easy steps, you can integrate the PhotoRoom Background Removal API into your Python project and provide your users with high-quality images with clean backgrounds.
According to resellers who use the PhotoRoom app, this feature can increase sales by 20 up to 100%.