rihekopo
rihekopo

Reputation: 3350

Loading image file as a layer to Photoshop

I have an existing PSD file that contains two images. My goal is to change one of the images to another image located in the same folder. I checked other questions and discovered that the same result can be achieved with Python in different ways, however I couldn't find the solution for my desired implementation. (I would like to do this by Photoshop's COM interface)

Actually, I can access to the layers and set their properties, but can't change the source image/object.

import win32com.client
import os

# opens ps
psApp = win32com.client.Dispatch("Photoshop.Application")
# opens file
psApp.Open(r"F:\dev\ae\f1.psd") 

# new image to add
img1 = r"F:\dev\ae\img1.png"

doc = psApp.Application.ActiveDocument

# new blank layer
doc.ArtLayers.Add()

# get layer to change
layer = doc.ArtLayers[1]        

Adobe has scripting guides for various languages and I figured out that most of the method and property names in Python are based on the VBScript version, but I couldn't figure out the solution even with the docs. When I call the Add() method on ArtLayers it creates a blank layer and I didn't find another method to do the same with an image/smartobject.

Upvotes: 2

Views: 2250

Answers (1)

Aaron Jiang
Aaron Jiang

Reputation: 46

Hey I was looking for the same thing and after digging through sample code in the docs you found, this is the work around I found.

psApp.Load(img1)
psApp.ActiveDocument.Selection.SelectAll()
psApp.ActiveDocument.Selection.Copy()
psApp.ActiveDocument.Close()
psApp.ActiveDocument.Paste()

I mean it is a bit stupid but it works. You might have to re-select the document you were on before pasting if it is not the last one.

Upvotes: 3

Related Questions