Code Monkey home page Code Monkey logo

Comments (10)

loonghao avatar loonghao commented on June 14, 2024 1

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)

from photoshop-python-api.

hittang avatar hittang commented on June 14, 2024

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

from photoshop-python-api.

loonghao avatar loonghao commented on June 14, 2024

@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)

from photoshop-python-api.

loonghao avatar loonghao commented on June 14, 2024

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

from photoshop-python-api.

ConnorCampagnaDeveloper avatar ConnorCampagnaDeveloper commented on June 14, 2024

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

from photoshop-python-api.

loonghao avatar loonghao commented on June 14, 2024

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?

from photoshop-python-api.

ConnorCampagnaDeveloper avatar ConnorCampagnaDeveloper commented on June 14, 2024

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 :)

from photoshop-python-api.

ConnorCampagnaDeveloper avatar ConnorCampagnaDeveloper commented on June 14, 2024

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)

from photoshop-python-api.

Gaston2388 avatar Gaston2388 commented on June 14, 2024

@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!!!

from photoshop-python-api.

chetan-SMRI avatar chetan-SMRI commented on June 14, 2024

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

from photoshop-python-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.