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

Ale*_*ogl 0 javascript photoshop photoshop-script

我有很多像这样的图像,需要批量处理:

未处理的图像

我想要完成的是裁剪掉每一侧的边框,但这里有一个问题:修剪不起作用,因为图像每个角的颜色不是黑色而是白色,所以我尝试裁剪每边距约 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);


}
Run Code Online (Sandbox Code Playgroud)

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

处理后的图像

Rob*_*obC 5

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

\n\n

无需访问activeDocument.activeLayer,因为裁剪会影响整个文档(即画布)。

\n\n

示例要点:

\n\n
var document = app.activeDocument; // Assumes a document is active.\n\n// Obtain original ruler units prefs.\nvar originalRulerUnits = app.preferences.rulerUnits;\n\n// Set the ruler units prefs to pixels.\napp.preferences.rulerUnits = Units.PIXELS;\n\n/**\n * Crops the canvas by x number of pixels equally from all sides.\n * @param {Number} [amountOfPixels=0] - Number of pixels to crop.\n */\nfunction cropCanvas(amountOfPixels) {\n    amountOfPixels = amountOfPixels || 0; // Defaults to zero.\n\n    // Obtain height and width of canvas.\n    var canvasWidth = document.width.value;\n    var canvasHeight = document.height.value;\n\n    // Define the new bounds.\n    var newBounds = [\n        amountOfPixels,\n        amountOfPixels,\n        canvasWidth - amountOfPixels,\n        canvasHeight - amountOfPixels\n    ];\n\n    // Crop the canvas. \n    document.crop(newBounds);\n}\n\n// Invoke the `cropCanvas` function passing\n// in the `amountOfPixels` value.\ncropCanvas(5);\n\n// Reset ruler prefs.\napp.preferences.rulerUnits = originalRulerUnits;\n
Run Code Online (Sandbox Code Playgroud)\n\n

笔记:

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

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

    \n\n
    cropCanvas(5); // Crops 5 x pixels from all sides.\n\ncropCanvas(25); // Crops 25 x pixels from all sides.\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
  5. 最后,Photoshop 的标尺单位将重置为其原始单位。

  6. \n
\n\n
\n\n

其他注意事项

\n\n

app.preferences.rulerUnits = Units.PIXELS;根据您的问题中所示的示例,不设置可能会导致不正确的裁剪。然而,当 Photoshop 的标尺原点的坐标设置为 时,它也会产生意想不到的结果(0,0)

\n\n

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

\n\n
    \n
  1. 打开图像 PhotoShop。
  2. \n
  3. Command通过键入+ R(macOS)Control+ (Windows)显示标尺R
  4. \n
  5. 通过将指针/光标定位在窗口左上角标尺的交叉点上,然后沿对角线向下拖动到图像上,更改标尺\xe2\x80\x99s 零原点。将出现一组十字线,标记标尺的新原点。这里进一步解释
  6. \n
  7. 然后运行上面提供的示例要点。
  8. \n
\n\n

如您所见,当标尺的零原点未设置为零时,可能会发生不需要的裁剪。

\n\n

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

\n\n

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

\n\n

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

\n\n

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

\n\n
var document = app.activeDocument; // Assumes a document is active.\n\n// Obtain original ruler units prefs.\nvar originalRulerUnits = app.preferences.rulerUnits;\n\n// Set the ruler units prefs to pixels.\napp.preferences.rulerUnits = Units.PIXELS;\n\n/**\n * Photoshop API doesn\'t provide a method to reset the ruler origin to [0, 0].\n * This get the cuurent ruler origin so we can offset the value.\n * @returns {Object} with properties `x` and `y` offset for rulers.\n */\nfunction getRulerOffset() {\n    var ref = new ActionReference();\n\n    ref.putEnumerated(\n        charIDToTypeID("Dcmn"),\n        charIDToTypeID("Ordn"),\n        charIDToTypeID("Trgt")\n    );\n    var desc = executeActionGet(ref);\n\n    var rulerPositionX = desc.getInteger(stringIDToTypeID(\'rulerOriginH\')) / 65536;\n    var rulerPositionY = desc.getInteger(stringIDToTypeID(\'rulerOriginV\')) / 65536;\n\n    return {\n        x : rulerPositionX,\n        y : rulerPositionY\n    }\n}\n\n/**\n * Crops the canvas by x number of pixels equally from all sides.\n * @param {Number} [amountOfPixels=0] - Number of pixels to crop.\n */\nfunction cropCanvas(amountOfPixels) {\n    amountOfPixels = amountOfPixels || 0; // Defaults to zero.\n\n    // Obtain height and width of canvas.\n    var canvasWidth = document.width.value;\n    var canvasHeight = document.height.value;\n\n    // Obtain current ruler x and y offset.\n    var rulerOffsetX = getRulerOffset().x;\n    var rulerOffsetY = getRulerOffset().y;\n\n    // Define the new bounds.\n    var newBounds = [\n        amountOfPixels - rulerOffsetX,\n        amountOfPixels - rulerOffsetY,\n        canvasWidth - amountOfPixels - rulerOffsetX,\n        canvasHeight - amountOfPixels - rulerOffsetY\n    ];\n\n    // Crop the canvas. \n    document.crop(newBounds);\n}\n\n// Invoke the `cropCanvas` function passing\n// in the `amountOfPixels` value.\ncropCanvas(5);\n\n// Reset ruler prefs.\napp.preferences.rulerUnits = originalRulerUnits;\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
    \n
  1. 一个新函数,即getRulerOffset,返回标尺原点的当前坐标x和坐标。y不幸的是,Photoshop 没有提供可以调用来重置标尺原点的 API,因此我们必须获取当前值。

  2. \n
  3. cropCanvas函数中,我们现在另外创建两个新变量(rulerOffsetXrulerOffsetY),并且在定义变量时将它们的值减去newBounds

  4. \n
\n\n
\n\n

使用该resizeCanvas()方法的另一种解决方案

\n\n

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

\n\n

这是一个要点:

\n\n
var document = app.activeDocument;\n\n// Obtain original ruler units prefs.\nvar originalRulerUnits = app.preferences.rulerUnits;\n\n// Set the ruler units prefs to pixels.\napp.preferences.rulerUnits = Units.PIXELS;\n\n/**\n * Resizes canvas removing x number of pixels equally from all sides.\n * @param {Number} [amountOfPixels=0] - Number of pixels to remove.\n */\nfunction removePixelsFromAllSides(amountOfPixels) {\n    amountOfPixels = amountOfPixels || 0;\n\n    document.resizeCanvas(\n       document.width.value - (amountOfPixels * 2),\n       document.height.value - (amountOfPixels * 2),\n       AnchorPosition.MIDDLECENTER\n    );\n}\n\n// Invoke the function passing in the `amountOfPixels` value.\nremovePixelsFromAllSides(5);\n\n// Reset ruler prefs.\napp.preferences.rulerUnits = originalRulerUnits;\n
Run Code Online (Sandbox Code Playgroud)\n