In this article, I want to introduce you to Photoshop Scripting in Python on Windows. For all of you Mac users, there is a way but it involves knowledge in AppScript which I don’t have and therefore can’t teach. If you’re a Windows user or if you have Windows on a partition of your Mac, you will just need any version of Photoshop installed to follow this tutorial.
I needed to programmatically create multiple images from a Photoshop template and have each different file output the template with a different text inside one of its text layers.
The goal here is going to be to use a basic Photoshop template with multiple text layers, edit the text value of said layers and export the result in a PNG format. With the use of functions and loop, I will also show you how I automatically create hundreds of images at once. Ready? Let’s do it.
Open Photoshop with Python
The first thing we want to do is to launch Photoshop. To give the order, we are going to need the Win32 COM and the OS module. With that imported, we can launch our Photoshop application and store it in a variable that we will call psApp like so:
You probably noticed by now, but executing the code presented above will trigger the launch of your Photoshop application. Now that this is done, we can move on and open our .psd file that will act as the base/template for all of the variants we are going to create.
To open a .psd file in Python, we just need to call the class “Open” of our psApp object and reference the path of your .psd file like so:
This piece of code, once executed, should open your template in your Photoshop instance.
How to Change the Text Content of a Photoshop Layer with Python
Now we need to create the object containing all of your layers. Let’s call it “doc”, for document.
The next step now is to look for the layers you’re interested in editing and explore their content. In this case, I want to access the content of the layer “Facts” so I can eventually edit it. To do so, we need to call the class “ArtLayers” of the variable storing the document and then call an other class that will allow us to edit its content. Follow these steps:
Change the Size of the Text Layer with Python
Let’s say you realize some of your text does not fit in your original layer. The ideal solution here would be to be able to change the font size. Well, we can do just that with a simple command like shown here:
PNG Export in Photoshop with Python
After having done the edits we wanted to do, it is now time to export our result in a web-friendly format. To proceed with this step, we need to determine the parameters of the export and provide the path and name of the file we want to output. Here is the code required to follow this step:
Once all of these variables have been set, we can export our file using this line of code:
And here is the file in all its glory:
Create Variants based on a Photoshop Template with Python
Now that we know how to change the text of a layer in Photoshop with Python and how to export the result, it is time for us to set ourselves to create tens of files in the blink of an eye. I will share here how you can create hundreds of images the same way I did with my now defunct Instagram page Esports Facts depicted below.
The text you can see in these images has been pulled out of the API from the website Esports Earnings. For the benefit of simplicity, we will simply create a dictionary that contains some text and create a loop together.
The first thing we want to do here is create a dictionary with some samples values like this:
Once this is done, we can create a simple loop that will:
- Replace the content of the text layer by the content of the value in the loop.
- Define a name for the file that is going to be exported.
- Export the file
And that’s how the code looks. Now let’s deconstruct this piece by piece.
Line 1 and 2 are here to select on which layer the code is going to work on. The layer “Facts” is the text layer that is hosting the text I am looking to edit.
Line 4,5 and 6 are the PNG Export parameters we’re going to give to the program so it knows in which format it should export the file.
Line 8 is the location of the folder where we want the files to end up. The full pathname is going to be constructed based on this root location later.
We then enter the loop. For every key and its value present in the dictionary, line 16 changes the content of the text layer “Facts” to the value of the key the loop has been presented with.
Line 18 is pretty important because it is creating the full path where the export will take place and under which name the file will be saved as. Here, the name of the file becomes the key of the dictionary in the loop with the “.png” extension.
Finally, line 20 actually proceed to export the result following the parameters set in line 4,5 and 6 and the fileName set in line 18. Rinse and repeat for every key/value combination present
And here are the results:
As you can see, everything worked as planned 🙂
And that’s pretty much it for me. As you may have noticed, this is a pretty basic edit but I’m confident this should help a good amount of people out there that were in the same boat as I was. If you’re looking to learn more, don’t hesitate to reach out to me in the comments and ask me what kind of edits you would like to automate with Python in Photoshop and I will try and make a tutorial related to these edits.
Once again, if you have any questions, feel free to email me or post a comment down in the section below.
Talk to you soon.