Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add a image to a layer? #38

Closed
hittang opened this issue Sep 18, 2020 · 10 comments
Closed

How to add a image to a layer? #38

hittang opened this issue Sep 18, 2020 · 10 comments

Comments

@hittang
Copy link

hittang commented Sep 18, 2020

How to add a image onto a layer? How to change a image of a layer? Thanks

@loonghao
Copy link
Owner

Hi @hittang you can try the below code, let me know if that can help you, cheers.

from photoshop import Session

with Session(action="new_document") as ps:
    desc = ps.ActionDescriptor
    desc.putPath(ps.app.charIDToTypeID("null"), "your/image/path.jpg")
    event_id = ps.app.charIDToTypeID("Plc ")  # `Plc` need one space in here.
    ps.app.executeAction(event_id, desc)

@hittang
Copy link
Author

hittang commented Sep 18, 2020

Thank you ,i will try the code, and tell you the result. another question, can you show a sample how to replace an image of layer by a new image with same size? or ,where the document or tutorial of your above code . tks again

@loonghao
Copy link
Owner

@hittang I usually search for the corresponding javascript fragment in google or get the javascript from ScriptingListener and then convert it use into python code use photoshop-python-api

the below code is replacing the current active image layer of the active document.

If the size of the two pictures is the same, there is no need to consider the size issue. If they are not the same, you may have to get the original picture size and replace it with a new picture and then resize it again.


Code from ScriptingListener

var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
    var desc93 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    desc93.putPath( idnull, new File( "C:\\Users\\hao.long\\Pictures\\HAL-1.png" ) );
executeAction( idplacedLayerReplaceContents, desc93, DialogModes.NO );

PYPS

from photoshop import Session

with Session() as ps:
    idplacedLayerReplaceContents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
    desc = ps.ActionDescriptor
    idnull = ps.app.charIDToTypeID("null")
    desc.putPath(idnull, "your/image/file.jpg")
    ps.app.executeAction(idplacedLayerReplaceContents, desc)

@loonghao
Copy link
Owner

Hi @hittang I just want to confirm that is all works for you?

@loonghao loonghao added this to Review in progress in photoshop-python Sep 30, 2020
photoshop-python automation moved this from Review in progress to Done Jan 10, 2021
@ConnorCampagnaDeveloper

Hi, is there a way i can change the image on a specific layer? Thanks

@loonghao
Copy link
Owner

Hi, is there a way i can change the image on a specific layer? Thanks

@ConnorCampagnaDeveloper
Do you give me more contextual information?
Do you mean to replace the image by replacing the layer?

@ConnorCampagnaDeveloper

Hi, is there a way i can change the image on a specific layer? Thanks

@ConnorCampagnaDeveloper
Do you give me more contextual information?
Do you mean to replace the image by replacing the layer?

for instance if I have a layer called "Player" and i have another layer called "Game", they both contain images, but i want to change the image of both these to 2 another 2 images. so instead of me changing the image on the active layer i would like to change it on another layer which i do not have selected.

Thanks :)

@ConnorCampagnaDeveloper
Copy link

ConnorCampagnaDeveloper commented Aug 1, 2021

Just realised i have not made that the clearest, basically what im trying to do is change an image on a layer that is not the active one selected.

https://imgur.com/zR2ubtG

click on image to see context of caption below

for example i would like to change the image in the SFC layer(context in image)

@Gaston2388
Copy link

@hittang I usually search for the corresponding javascript fragment in google or get the javascript from ScriptingListener and then convert it use into python code use photoshop-python-api

the below code is replacing the current active image layer of the active document.

If the size of the two pictures is the same, there is no need to consider the size issue. If they are not the same, you may have to get the original picture size and replace it with a new picture and then resize it again.

Code from ScriptingListener

var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
    var desc93 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    desc93.putPath( idnull, new File( "C:\\Users\\hao.long\\Pictures\\HAL-1.png" ) );
executeAction( idplacedLayerReplaceContents, desc93, DialogModes.NO );

PYPS

from photoshop import Session

with Session() as ps:
    idplacedLayerReplaceContents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
    desc = ps.ActionDescriptor
    idnull = ps.app.charIDToTypeID("null")
    desc.putPath(idnull, "your/image/file.jpg")
    ps.app.executeAction(idplacedLayerReplaceContents, desc)

Hello!!
I'm trying to use the code to load images in a layer but it doesn't work for me.
I get the following error:

File "C:\Python39\lib\site-packages\comtypes\automation.py", line 745, in _invoke
self.__com_Invoke(memid, riid_null, lcid, invkind,
_ctypes.COMError: (-2147212704, None, (None, None, None, 0, None))

Any idea why it could be?
Thanks!!!

@chetan-SMRI
Copy link

hello
i am doing same thing , i tried both of your codes, but in my case i am using exactly same image as layer holder,

when i manually do this in photoshop it works fine(i dont have to do any placement because it is exactly as same pixel as layer), but when i do it through the code, the photo becomes more wider than i thought

code i used --
with Session(mockup, action="open",auto_close=True) as ps: active_layer = ps.active_document.activeLayer bounds = active_layer.bounds # in this segment we are getting the frame of mockup, then we use # our function to adjust the image within given pixel without distortion width = int(bounds[2] - bounds[0]) height = int(bounds[3] - bounds[1]) adjust_image()# i replace my image here with same width and height as layer replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents") desc = ps.ActionDescriptor idnull = ps.app.charIDToTypeID("null") desc.putPath(idnull, 'img.png') ps.app.executeAction(replace_contents, desc) ps.active_document.saveAs(f'res.jpg', ps.JPEGSaveOptions())

i am attaching results for reference -
using code -
using code
manually -
manual result
mockup file i used - link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

5 participants