Ollie Rowles
Ollie's Blog

Ollie's Blog

🎨 Using Python Scripts To Edit Photoshop Layers

🎨 Using Python Scripts To Edit Photoshop Layers

Ollie Rowles's photo
Ollie Rowles
·Feb 6, 2022·

3 min read

Table of contents

  • Pre-requisites
  • Within the editor
  • More Information

I was trying to find out how to create multiple copies of the same image but insert a different line of text from a bank of lyrics I had created. I then found that using Photoshop you can quite easily manipulate your files with Python, saving them locally in a matter of minutes.

This could be used to generate copies of social media adverts/snippets, creating artwork with random features of colour, the API is quite expansive with what you can change as you are essentially using the Photoshop client but manipulating it with code and this guide will explain how...


You will need to following tools to be installed: Python (3.9), Adobe Photoshop, and Pip package (win32)

I will assume that the user has Python and Photoshop installed locally and requires no guidance on this, however, a quick Google search will help you there. Furthermore, if you don’t have a Creative Cloud subscription - quite often Adobe gives free trials you can use, as I did.

Once Python is installed within a terminal window with administrator privileges type in the command:

pip install -U pypiwin32

  • NOTE: There is multiple different methods to install packages

Within the editor

Open up an editor with a .py file and to manipulate the PSD file you need to use the win32 package to open Photoshop and locate the folder/file you are using by the following:

import win32com.client

// Open Adobe Photoshop App programmatically and locate PSD file
psApp = win32com.client.Dispatch("Photoshop.Application")
// Import layers
layers = psApp.Application.ActiveDocument

The next lines of code can be changed to your specification but here we will be using a text file to create an image, with each line of text replacing a textbox field within the PSD file. This is done using a for loop taking each line in the .txt file.

// Open text file, ENCODING may need changing/removed
file1 = open("C:\\Users\\olive\\FULL_PATH_TO_FILE.txt", encoding="utf8")

// Will take each line of the text file above
for line in file1:
    // Grab the layer by name for what you want to manipulate in PS
    quote_layer = layers.ArtLayers["Text-Line"]
    // Inserts the text line into the image
    quote_layer.TextItem.contents = line.strip()

    // Export as JPG
    options = win32com.client.Dispatch('Photoshop.ExportOptionsSaveForWeb')
    options.Format = 6
    options.Quality = 100

    // Export the file to system
    fileName=("C:\\Users\\olive\\OUTPUT_FOLDER_PATH\\FILE_NAME_" + str(count) +".jpg")
    layers.Export(ExportIn=fileName, ExportAs=2, Options=options)
    // Create Console log to see progress
    print("Image saved no:" + str(count))

You will notice that on running the script photoshop will open and begin manipulating the layer called ‘Text-Line’ in this case, then save the images as jpegs after each iteration to your output called fileName. You can just let this loop run through until completion!

More Information

What I have done is manipulate one layers text context, but if you wish to build on this, you can read the documentation on using Photoshop in scripting here.

Share this