How to process images from an Excel spreadsheet
A common use case for the Image Editing API is to process a large amount of images, whose URLs are stored inside an Excel spreadsheet.
In this tutorial, we'll see how it's possible to process these images through a Python script that will:
parse the Excel spreadsheet
make the calls to the Image Editing API
save the result images to the disk.
Let's take the example of this spreadsheet:

Step 1: Calling the Image Editing API
First, we'll write a function that can process a single image through the Image Editing API and save the result image returned by the API to the disk:
This code is pretty straightforward:
it configures the parameters of the call to the Image Editing API:
white background
output size of 1000x1000px
padding of 10%
uses the
requestslibrary to make the GET HTTP call to the APIsaves 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.
Step 2: Parsing the Excel spreadsheet
Now that we have a function to process a single image, the next step is to parse the content of the Excel spreadsheet, iterate over all the image URLs it contains and call process_image() for each of them.
The code above contains the first half of the function iterate_over_spreadsheet(), here are its important steps:
we open the spreadsheet located at
spreadsheet_pathusing the libraryopenpyxlwe iterate over all the sheets contained in the document
we look for the column whose first cell contains the value
column_name(in the case of the spreadsheet we use as an example, that value would be"Image URL")we iterate over all the rows in that column, and collect their
valuein the arrayimage_urls
Now that we have collected all the image_urls, we can write the second part of the function iterate_over_spreadsheet():
In this second part of the function we:
iterate over the
image_urlscompose the
result_pathwhere the result image will be savedcheck that a file doesn't already exist at
result_path(so as to not process the same image twice)call the function
process_image()through anexecutor, which allows us to execute 4 API calls in parallel
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:
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 whose URLs are store inside an Excel spreadsheet 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?