Cannot replace an image layer
Describe the bug I am trying to replace an image layer in a photoshop file. For that purpose I am using this next code:
To Reproduce This is my python code: with Session() as ps: replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents") desc = ps.ActionDescriptor idnull = ps.app.charIDToTypeID("null") desc.putPath(idnull, "G:\BLEBLE\BLABLABLA\BLABLABLA\BLABLABL\BLABLA\2022\August\Images\09.jpeg") ps.app.executeAction(replace_contents, desc)
Expected behavior It suppose to replace already existing image layer but instead of thar it show an error message and log.
Screenshots
Photoshop error message:

Terminal message:
Traceback (most recent call last):
File "g:\Blebleble\blablabla\blablabla\blablabla\Publicaciones\AUTOMATION\automate.py", line 43, in
Desktop (please complete the following information):
- OS: Windows 11 Home
- Photoshop Version: Adobe Photoshop Version: 22.5.4 20211208.r.631 a0cb269 x64
- Python Version: python-3.10
Additional context This code actually worked once but suddenly it stopped working.
Sorry but I can't help you directly. Though this happened to me too (including the code working only once), and I worked around it by simply importing the new image and getting rid of the old one in another way.
Sorry but I can't help you directly. Though this happened to me too (including the code working only once), and I worked around it by simply importing the new image and getting rid of the old one in another way.
Thanks, I will give it a try.
Have you tried to look at #174? What's missing in your code is setting the active layer: ps.active_document.activeLayer = layer. Maybe that'll work.
@Edimo05
You can try to add r in the file path, to ensure the code use raw path
desc.putPath(idnull, r"G:\BLEBLE\BLABLABLA\BLABLABLA\BLABLABL\BLABLA\2022\August\Images\09.jpeg")
@Edimo05 You can try to add
rin the file path, to ensure the code use raw pathdesc.putPath(idnull, r"G:\BLEBLE\BLABLABLA\BLABLABLA\BLABLABL\BLABLA\2022\August\Images\09.jpeg")
@loonghao I tried and doesn't work. Still getting the same error.
@loonghao I tried this code (https://loonghao.github.io/photoshop-python-api/examples/#replace-images) but I cannot import examples._psd_files. I already installed photoshop_python_api.
Have you tried to look at #174? What's missing in your code is setting the active layer:
ps.active_document.activeLayer = layer. Maybe that'll work.
@blunderedbishop I tried change my code to this: with Session() as ps: ps.active_document.activeLayer = doc.ArtLayers["08"] replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents") desc = ps.ActionDescriptor idnull = ps.app.charIDToTypeID("null") desc.putPath(idnull, r"G:\BLEBLE\BLABLABLA\BLABLABLA\BLABLABL\BLABLA\2022\August\Images\09.jpeg") ps.app.executeAction(replace_contents, desc)
and it shows next error:
Traceback (most recent call last):
File "g:\Blebleble\blablabla\blablabla\blablabla\Publicaciones\AUTOMATION\automate.py", line 40, in
maybe I placed that code u mentioned wrong?
@Edimo05 ArtLayers is a list, you need artLayer + index to access it
doc.ArtLayers["08"]
with Session() as ps:
ps.active_document.activeLayer = doc.artLayers.getByName("08")
replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
desc = ps.ActionDescriptor
idnull = ps.app.charIDToTypeID("null")
desc.putPath(idnull, r"G:\BLEBLE\BLABLABLA\BLABLABLA\BLABLABL\BLABLA\2022\August\Images\09.jpeg")
ps.app.executeAction(replace_contents, desc)
@Edimo05 ArtLayers is a list, you need artLayer + index to access it
doc.ArtLayers["08"]with Session() as ps: ps.active_document.activeLayer = doc.artLayers.getByName("08") replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents") desc = ps.ActionDescriptor idnull = ps.app.charIDToTypeID("null") desc.putPath(idnull, r"G:\BLEBLE\BLABLABLA\BLABLABLA\BLABLABL\BLABLA\2022\August\Images\09.jpeg") ps.app.executeAction(replace_contents, desc)
@loonghao now is showing this error:
Traceback (most recent call last):
File "g:\Blebleble\blablabla\blablabla\blablabla\Publicaciones\AUTOMATION\automate.py", line 55, in
@Edimo05
doc.ArtLayers.getByName("08")
ArtLayers first letter lowercase
doc.artLayers.getByName("08")
@Edimo05
doc.ArtLayers.getByName("08")
ArtLayersfirst letter lowercasedoc.artLayers.getByName("08")
@loonghao nothing man, same error:
Traceback (most recent call last):
File "g:\Blebleble\blablabla\blablabla\blablabla\Publicaciones\AUTOMATION\automate.py", line 75, in
@Edimo05
replace image only support ArtLayer as a smart object layer

Please convert the 08 layer as a smart-object first and then execute the following code
from photoshop import Session
with Session() as ps:
doc = ps.active_document
doc.activeLayer = doc.artLayers.getByName("08")
replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
desc = ps.ActionDescriptor
idnull = ps.app.charIDToTypeID("null")
desc.putPath(idnull, r"G:\BLEBLE\BLABLABLA\BLABLABLA\BLABLABL\BLABLA\2022\August\Images\09.jpeg")
ps.app.executeAction(replace_contents, desc)
@Edimo05 replace image only support ArtLayer as a smart object layer
Please convert the 08 layer as a smart-object first and then execute the following code
from photoshop import Session with Session() as ps: doc = ps.active_document doc.activeLayer = doc.artLayers.getByName("08") replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents") desc = ps.ActionDescriptor idnull = ps.app.charIDToTypeID("null") desc.putPath(idnull, r"G:\BLEBLE\BLABLABLA\BLABLABLA\BLABLABL\BLABLA\2022\August\Images\09.jpeg") ps.app.executeAction(replace_contents, desc)
@loonghao finally works. That layer was already an smart object, I think I missed ps.active_document. Thanks a lot!!!
@Edimo05 good to hear, closing issue.