The Photoshop API is an incredibly powerful REST-based API for performing Photoshop tasks in the cloud and at scale.
I’ve been demonstrating its features on our blog over the past few months, generally using Node.js, but the API’s simplicity makes it available for any platform out there.
In this article, I’ll share an example of how Python can be used to interact with the Photoshop API. As a warning, I’m fairly new to Python, but one of the best aspects of the language is just how easy it is to pick up and start using it right away.
If you haven’t already done so, grab your free trial credentials so you can play with the API yourself. With that, let’s get started!
In general, working with the different parts of the Photoshop APIs involves much the same steps:
- Convert credentials into an access token
- Determine input and output URLs (as the APIs work with cloud storage, this will depend on your storage system and their SDKs)
- Call the API endpoint, passing in your URLs and other arguments required for that particular service
- Check the result of the API call on a schedule to determine the status of the job
So Let’s break these down into individual Python examples.
Again, assuming you have your credentials, getting an access token is as simple as a call to Adobe’s ims
endpoint. Here’s an example:
Note that the assumption is that the credentials, CLIENT_ID
and CLIENT_SECRET
are available in environment variables. Running this script will dump out the result of the call:
Typically you can just work with the access_token
result of the call.
The next step is completely optional, and not generally recommended in production, but it may be a good idea while learning and testing.
The Photoshop API provides a method that only verifies the access token. This is handy as other calls will require working with cloud storage. The hello
endpoint will do simply that: echo back a greeting if you’ve correctly performed authentication. Let’s update the Python call with a new function testing that, sayHello
:
All of our API calls will require two headers for authorization. The first, Authorization
, includes the access token returned from the earlier request, while x-api-key
uses the CLIENT_ID
value from our credentials.
The result of this script is simply: Response from hello endpoint: Welcome to the Photoshop API!
The next thing to do is start a job of some sort. The Photoshop API has many features, but for today, let’s consider the Remove Background API which, you guessed it, removes the background from an image.
In order to do this, we need both a source for our input and a place to store the output.
The Photoshop APIs support cloud storage on Amazon S3, Dropbox, and Azure, and the particularities of each depend on their SDKs. For today, let’s just consider a simple S3 bucket. The Photoshop API team created a nice Python demo I can borrow from to simplify making those S3 URLs using the Python boto3
package.
Let’s look at our updated script:
There are two big changes here. First note the use of get_presigned_url
. This lets us create a readable URL for our source image as well as a writeable URL for the output. You can see that towards the bottom of the script where inputURL
and uploadURL
are created.
Next, the create_removebg_job
function is called with our credentials and URLs. In that function, look at data
. This information is passed to the API and defines how it should operate. In general, every single part of the API will be a version of this, with just the parameters changing based on what operation is being used.
The end result of this call is a job
that looks like this:
For the final part of our sample code, let’s check the job and keep checking it until it finishes.
Technically, you don’t need to do this in the same script. You could, for example, store the job result in a database and use a scheduled process to check the result at a later time. Since the “typical” call to the Photoshop API will finish rather quickly, it isn’t too difficult to add it to our script.
In the code sample below, I’m only sharing the portion where the job is created and how polling for the result is handled. All the code before this portion is the same as before:
Now we’re taking the URL from the job creation call and continuing to call it until the status of the response is either good or bad. In that loop, the response is also bring printed out. Here’s an example of what that looks like when done:
If you would like to play with this code, you can find it on my repository here: https://github.com/cfjedimaster/photoshop_api_preso/tree/main/demos/python. Also, you can find a notebook version of it here, but be sure to note the requirements for credentials at the top.
Let us know how this works for you, and reach out on our forums with any questions!