skip to Main Content

I would like to open a template file such as "shirt_mockup.psd" and replace the image on a given layer such as "replacement_layer" to produce a mockup like "mockup.png". While this is easy in Photoshop, a better method is needed to process a large number of images for the same template in my application. It’s also necessary to do this without opening Photoshop

Attempts:
psd-tools library is good but non-writable (as far as I can tell). photoshop_python_api library appears very complex and seems to depend on winreg for python2, while I’m using python3 in my application. Open to alternative approaches for automating the mockups from a template!

Pseudocode:

from psd_tools import PSDImage
from PIL import Image

def insert_image_into_psd(psd_file, layer_name, insert_image_file, output_file):
    psd = PSDImage.open(psd_file)

    for l in psd:
        if l.name == layer_name:
            insert_image = Image.open(insert_image_file).convert("RGBA")
            resized_image = insert_image.resize(l.size, resample=Image.LANCZOS)
            l.topil().paste(resized_image, box=(0, 0))
            break

    psd.save(output_file)
    psd.composite().save(output_file + ".png")
    print("Image inserted into the specified layer and saved as PSD and PNG.")

psd_file = "template.psd"
layer_name = "your design here - doubleclick on thumnail"
insert_image_file = "replacement.png"
output_file = "output.psd"

insert_image_into_psd(psd_file, layer_name, insert_image_file, output_file)

2

Answers


  1. I have the same issue.
    I found SmartObject class in link
    I want to use this class to solve this issue.
    If you solved your issue, hope for your advice.
    Thanks.

    Login or Signup to reply.
  2. So you can automate the batch replacement of your Smart Objects to output image files, like you’re asking. However to my knowledge, there is no way to work + run commands inside of Photoshop without having Photoshop open + running. There are some operations it can run while minimized, but as far as I know, the application must at least be running on your computer to perform these operations.

    Now in terms of how to automate this process effectively, you have a few options:

    OPTION 1) There’s a Photoshop plugin called Batch Replace Smart Objects that basically does exactly what you’re asking: You specify the PSD document you want to use, and it’ll replace the Smart Object with all the images from your input folder(s), and export the updated Photoshop document as an image file for each one. I have used this to automate exactly what you’re asking, and I’ve also tested it out with Photoshop minimized. Even for complex operations where it needs to run multiple steps (ie, if I have it create mockup images using 7 different PSD documents / mockup templates), I’ve had it run completely in the background with Photoshop minimized and it has worked effectively.

    Honestly I don’t know if any Photoshop operations are designed to be run as minimized background processes like this, and your safest bet is to always run these operations with Photoshop as the active window if you want to maximize your odds of it working properly — but I have tested this method out with Photoshop minimized, on Windows 10, and it did work to automate this process. If you’re comfortable running this kind of mockup automation with Photoshop as the open + active window, this method should definitely work.

    OPTION 2) You could also write a Photoshop script to do this. I’ve also used this method to effectively automate this process — however I’ve never tested it with Photoshop minimized, and I do not know if this method would work if you absolutely needed Photoshop to not be the active window while this operation runs. With that said, you could test it out to see what happens — and if you search the Adobe Community forums, there are some posts on there where people ask similar questions. These might not be the exact operations you’re trying to perform, however as long as you have experience writing Javascript programs, you should be able to modify the code you find on there for your specific workflow needs.

    If you need to customize the scripts for your operations, this is a good repository that has the official Adobe Scripting guides. You can search through these to find whatever operations you need — then just update the .jsx code with the modifications to make it work how you need it to.

    Another option would be, use a Script Listener tool. This allows you to perform certain operations inside of Photoshop, then it’ll log the Action Reference code that Photoshop uses to run the commands. You can then convert this into the .jsx code that you need to actually automate what you’re going for via Photoshop scripting. That’s another popular approach I see people take — and I’ve also used this approach to help identify the Photoshop Scripting code I need to actually automate what I’m trying to do.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search