How to process images from a local directory

A common use case for the Image Editing API or the Remove Background API is to process a large amount of images, which are stored in a local directory.

In this tutorial, we'll see how it's possible to process these images by integrating the Image Editing API or the Remove Background API with a Python script.

Step 1: Calling the API

First, we'll write a function that can process a local image through the Image Editing API or the Remove Background API and save the result image returned by the API to the disk:

import requests

API_KEY = "REPLACE_WITH_YOUR_API_KEY"

def process_image(input_image_path, output_image_path):
    try:
        url = "https://image-api.photoroom.com/v2/edit"

        with open(input_image_path, 'rb') as image_file:
            files = { "imageFile": image_file }

            headers = {
                "Accept": "image/png, application/json",
                "x-api-key": API_KEY
            }

            response = requests.post(url, files=files, headers=headers)
            response.raise_for_status()

            with open(output_image_path, 'wb') as f:
                f.write(response.content)
                print(f"Image downloaded and saved to {output_image_path}")

    except requests.RequestException as e:
        print(f"Error: {str(e)}")
        return str(e)

This code is pretty straightforward:

  1. it opens the image stored at input_image_path

  2. uses the requests library to make the POST HTTP call to the API

  3. saves the result image at output_image_path

Notice that you will need to update the value of the constant API_KEY with your own API key.

circle-info

If you don't have an API key, here are the steps to create yours.

Step 2: Iterating over a local directory

Now that we have a function to process a single image, the next step is to iterate over all the images stored in a local directory and call process_image() for each of them.

This time the code is a bit more complex, but here are its important steps:

  1. we iterate over the files inside the directory at input_directory_path

  2. then for each file we:

    1. compose the result_path where the result image will be saved

    2. check that a file doesn't already exist at result_path (so as to not process the same image twice)

    3. call the function process_image() through an executor, which allows us to execute 4 API calls in parallel

circle-info

If you want, you can increase the value of the argument max_workers to run more API calls in parallel. Keep in mind though that the API is rate limited to 60 calls/minutes.

Step 3: Running the script

We're almost there, the last thing we need is to actually run the script.

To do this we'll add this final piece of code:

Here we set the constants INPUT_DIRECTORY and OUTPUT_DIRECTORY to the paths of respectively:

  • the directory where the input images are stored

  • the directory where we want to save the result images.

Then all that's left is to actually run the script using the terminal:

Conclusion

In this tutorial, we saw how to use a Python script to easily process images stored locally with the Photoroom API.

Download the code sample

Here's the entire code sample, if you want to easily save it to a file:

Last updated

Was this helpful?