Photoshop脚本从文档的每一侧裁掉x像素

时间:2018-01-02 12:38:48

标签: javascript photoshop photoshop-script

我有很多像这样的图像需要作为批处理来处理:

unprocessed image

我想要完成的是剪掉各边的边框,但这里有一个问题:修剪不起作用,因为图像每个角落的颜色不是黑色而是白色,所以相反我是试图在每一侧裁掉大约5个像素。我已经写了一些代码来做到这一点,但似乎某处出现了错误导致有趣,镜像和错误裁剪的图像:

function cropLayerToBounds(){
var layer = activeDocument.activeLayer; //Grab the currently selected layer

// get height and width
var actualHeight = layer.bounds[2]-layer.bounds[0]; //Grab the height
var actualWidth = layer.bounds[3]-layer.bounds[1]; //Grab the width

// calculate desired height and width
var desiredHeight = actualHeight - 5;
var desiredWidth = actualWidth - 5;

// not sure if necessary
    var doc = app.activeDocument;
    var halfWidth = (desiredWidth/2);
    var halfHeight = (desiredHeight/2);
    var centerX = (doc.width/2);
    var centerY = (doc.height/2);

// error seems to be here

   // tried this
   var bounds = [0,0,desiredHeight,desiredWidth];

   // and this
   var bounds = [(centerX-halfWidth),(centerY-halfHeight),(centerX+halfWidth),(centerY+halfHeight)];

    doc.crop(bounds);


}

我以这种方式处理的图像看起来有点像这样:

processed image

1 个答案:

答案 0 :(得分:3)

以下脚本使用名为cropCanvas的自定义函数,该函数将满足您的要求。

没有必要访问activeDocument.activeLayer,因为裁剪会影响整个文档(即画布)。

示例要点:

var document = app.activeDocument; // Assumes a document is active.

// Obtain original ruler units prefs.
var originalRulerUnits = app.preferences.rulerUnits;

// Set the ruler units prefs to pixels.
app.preferences.rulerUnits = Units.PIXELS;

/**
 * Crops the canvas by x number of pixels equally from all sides.
 * @param {Number} [amountOfPixels=0] - Number of pixels to crop.
 */
function cropCanvas(amountOfPixels) {
    amountOfPixels = amountOfPixels || 0; // Defaults to zero.

    // Obtain height and width of canvas.
    var canvasWidth = document.width.value;
    var canvasHeight = document.height.value;

    // Define the new bounds.
    var newBounds = [
        amountOfPixels,
        amountOfPixels,
        canvasWidth - amountOfPixels,
        canvasHeight - amountOfPixels
    ];

    // Crop the canvas. 
    document.crop(newBounds);
}

// Invoke the `cropCanvas` function passing
// in the `amountOfPixels` value.
cropCanvas(5);

// Reset ruler prefs.
app.preferences.rulerUnits = originalRulerUnits;

备注:

  1. 该脚本最初通过app.preferences.rulerUnits;获取Photoshop的当前标尺单位,并在将标尺单位设置为像素之前将其分配给originalRulerUnits变量。

  2. cropCanvas(...)函数有一个参数,即amountOfPixels。这使得可以使用一个参数调用函数,该参数指定要裁剪图像的像素数量。例如:

    cropCanvas(5); // Crops 5 x pixels from all sides.
    
    cropCanvas(25); // Crops 25 x pixels from all sides.
    
  3. 最后,Photoshop的标尺单位将重置为原始单位。

  4. 其他注意事项

    根据问题中显示的示例,未设置app.preferences.rulerUnits = Units.PIXELS;会导致裁剪错误。但是,如果

    为了说明这个潜在的问题,请执行以下操作:

    1. 打开图片PhotoShop。
    2. 通过键入 Command + R (macOS) Control + R来显示标尺 (Windows)中
    3. 通过将指针/光标放在窗口左上角的标尺交叉点上,并将对角线向下拖动到图像上,来更改标尺的零点原点。将出现一组十字线,标记标尺的新原点。进一步解释here
    4. 然后运行上面提供的示例要点。
    5. 如您所见,当标尺的零点原点未设置为零时,可能会发生意外的裁剪。

      通过双击标尺的左上角,可以将标尺原点重置为默认值(即(0,0))。

      重要提示: 现在,对于您来说这不应该是一个问题,正如您在问题中提到的那样" ...需要作为批处理。" ,这表明您的最终脚本将以编程方式逐个打开图像(即自动化)。打开图像时,标尺原点默认为0,0,因此不会发生意外裁剪。

      但是,在一种情况下,脚本的一个功能是允许它在用户打开的图像上运行,并且可能已定义了新的标尺原点。然后你的脚本还需要将标尺原点重置为零(即0,0)。

      处理非零标尺原点的示例要点:

      0,0

      修订后的脚本(上图)现在除了上一个要点之外还包括:

      1. 一个新函数,即var document = app.activeDocument; // Assumes a document is active. // Obtain original ruler units prefs. var originalRulerUnits = app.preferences.rulerUnits; // Set the ruler units prefs to pixels. app.preferences.rulerUnits = Units.PIXELS; /** * Photoshop API doesn't provide a method to reset the ruler origin to [0, 0]. * This get the cuurent ruler origin so we can offset the value. * @returns {Object} with properties `x` and `y` offset for rulers. */ function getRulerOffset() { var ref = new ActionReference(); ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); var desc = executeActionGet(ref); var rulerPositionX = desc.getInteger(stringIDToTypeID('rulerOriginH')) / 65536; var rulerPositionY = desc.getInteger(stringIDToTypeID('rulerOriginV')) / 65536; return { x : rulerPositionX, y : rulerPositionY } } /** * Crops the canvas by x number of pixels equally from all sides. * @param {Number} [amountOfPixels=0] - Number of pixels to crop. */ function cropCanvas(amountOfPixels) { amountOfPixels = amountOfPixels || 0; // Defaults to zero. // Obtain height and width of canvas. var canvasWidth = document.width.value; var canvasHeight = document.height.value; // Obtain current ruler x and y offset. var rulerOffsetX = getRulerOffset().x; var rulerOffsetY = getRulerOffset().y; // Define the new bounds. var newBounds = [ amountOfPixels - rulerOffsetX, amountOfPixels - rulerOffsetY, canvasWidth - amountOfPixels - rulerOffsetX, canvasHeight - amountOfPixels - rulerOffsetY ]; // Crop the canvas. document.crop(newBounds); } // Invoke the `cropCanvas` function passing // in the `amountOfPixels` value. cropCanvas(5); // Reset ruler prefs. app.preferences.rulerUnits = originalRulerUnits; ,它返回标尺原点的当前getRulerOffsetx坐标。不幸的是,Photoshop没有提供可以调用来重置标尺原点的API,所以我们必须改为获取当前值。

      2. y函数中,我们现在另外创建两个新变量(cropCanvasrulerOffsetX),并在定义rulerOffsetY变量时减去它们的值。

      3. 使用newBounds方法的另一种解决方案

        满足您要求的另一种方法是使用resizeCanvas()方法而不是resizeCanvas()方法。我实际上更喜欢这种方法,因为标尺原点的位置(如"其他注意事项" 部分所讨论的那样)没有任何影响。

        这是一个要点:

        crop()