loonghao / photoshop-python-api

Python API for Photoshop.
https://loonghao.github.io/photoshop-python-api/
MIT License
604 stars 68 forks source link

多次替换psd中同一个图片,并在每次替换后将替换后的psd保存为png。在这个过程的第二次开始,它会将上一次保存的png替换到想要替换的图层上,描述可能有点不清晰,代码如下 #276

Closed white-26 closed 1 year ago

white-26 commented 1 year ago
from photoshop import Session

def BatchChange(picPath, picName):
    with Session("D:/newT.psd", action="open") as ps:
        active_layer = ps.active_document.activeLayer
        bounds = active_layer.bounds
        replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
        desc = ps.ActionDescriptor
        idnull = ps.app.charIDToTypeID("null")
        desc.putPath(idnull, picPath)
        ps.app.executeAction(replace_contents, desc)

        # replaced image.
        active_layer = ps.active_document.activeLayer
        current_bounds = active_layer.bounds
        width = bounds[2] - bounds[0]
        height = bounds[3] - bounds[1]

        current_width = current_bounds[2] - current_bounds[0]
        current_height = current_bounds[3] - current_bounds[1]
        new_size = width / current_width * 100
        active_layer.resize(new_size, new_size, ps.AnchorPosition.MiddleCenter)
        active_document = ps.app.activeDocument
        active_document.saveAs(f"D:/psdResult/{picName}.png", ps.PNGSaveOptions(), asCopy=True)

for i in range(3):
    BatchChange(f"D:/pic_{i + 1}.png", f"pic_{i + 1}")
Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Title: Replace the same picture in psd multiple times, and save the replaced psd as png after each replacement. In the second start of this process, it will replace the last saved png with the body layer you want to replace, the description may be a bit unclear, the code is as follows