如何-Photoshop脚本-合并两层

时间:2019-08-19 17:14:12

标签: javascript merge photoshop layer extendscript

我的Photoshop画布是900X600。

下面的功能采用 X层并进行 X层复制

需要 X层复制,并在保持比例的同时将高度调整为600px。 var newdLayer

需要 Layer X ,并在保持比例的同时将宽度调整为900px并应用高斯模糊。 var blur

然后我要合并 X层 X层

显然有一个merge()函数,但是为此您必须创建一个layerset。并不是真正的JavaScript专业人士。

如何合并两层?

(function (){

var docRef = activeDocument
var newdLayer = docRef.activeLayer.duplicate();

newdLayer;

    var startRulerUnits = app.preferences.rulerUnits;  
    app.preferences.rulerUnits = Units.PIXELS;  
    var bounds = newdLayer.bounds;  
    var height = bounds[3].value - bounds[1].value;
    var newSize = (100 / height) * 600;  
    newdLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
    app.preferences.rulerUnits = startRulerUnits;  

var blur = docRef.activeLayer;

    var startRulerUnits = app.preferences.rulerUnits;  
    app.preferences.rulerUnits = Units.PIXELS;  
    var bounds = blur.bounds;  
    var width = bounds[2].value - bounds[0].value;
    var newSize = (100 / width) * 900;  
    blur.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
    app.preferences.rulerUnits = startRulerUnits;  
    blur.applyGaussianBlur(5)

blur;


})();

1 个答案:

答案 0 :(得分:1)

我通过消除一些多余的步骤来修改了您的功能,并在最后添加了一个代码段,以方便创建新的layerSet,将newdLayerblur层移到集合中并合并设置:

(function (){

var docRef = activeDocument
var blur = docRef.activeLayer;
var newdLayer = blur.duplicate();

    var startRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    // since we resize based on the initial size of the source layer, 
    // we don't need to get the bounds twice
    var bounds = blur.bounds;
    var height = bounds[3].value - bounds[1].value;
    var width = bounds[2].value - bounds[0].value;
    // declare 2 different vars for your sizes (there are better ways to do this, but
    // since you say you aren't a JavaScript pro, I figured I'd keep it simple)
    var newSize600 = (100 / height) * 600;
    var newSize900 = (100 / width) * 900;
    // resize your layers
    newdLayer.resize(newSize600, newSize600, AnchorPosition.MIDDLECENTER);
    blur.resize(newSize900, newSize900, AnchorPosition.MIDDLECENTER);
    // apply blur
    blur.applyGaussianBlur(5);

    // below creates the group, moves the layers to it and merges them. Feel free to just include this part
    // at the end of your function if you don't want to use the modified code above. 

    // create a new layer set
    var groupOne = docRef.layerSets.add();

    // move the blur layer inside the layer set and name the layer for posterity
    blur.move(groupOne, ElementPlacement.INSIDE);
    blur.name = "blur";

    // move the newdLayer inside and rename
    newdLayer.move(groupOne, ElementPlacement.INSIDE);
    newdLayer.name = "newdLayer";

    // merge the layer set and name the new layer
    var mergedGroup = groupOne.merge();
    mergedGroup.name = "newdLayer + blur";

    app.preferences.rulerUnits = startRulerUnits;

})();

请注意,merge()也可以使用artLayers方法,但是根据文档的层数和层顺序,它可能需要其他步骤才能合并使用artLayers方法创建2层。这是因为artLayers merge()方法只是将活动的任一层与其下一层合并。复制图层时,我相信它们将放置在artLayers集合中的索引0处,这将是“图层”调板中的最顶层。如果源图层尚未位于索引0,则需要对merge();

之前的图层进行排序