Photoshop 透明度修剪

问题描述

我是编程新手,现在我只能用 C# 编写代码。 我真的需要你的 Photoshop 修剪脚本的帮助。我已经搜索了几天,但找不到如何实现我想要的。 它是这样的。 我有一个从画布原点偏移的字符的 PNG 图像。 现在我想从左侧和右侧均等地修剪透明度,直到我从左侧或右侧(以先到者为准)点击第一个像素,然后停止修剪(这意味着由于从原点偏移)。顶部和底部也一样。几周以来我对此感到绝望,如果有人能帮助我,我会很高兴。 下面的示例图片

enter image description here

解决方法

例如:

  // save current preferences and make sure PS units are in pixels
  var startRulerUnits = preferences.rulerUnits
  preferences.rulerUnits = Units.PIXELS

  // initial variables
  var doc = activeDocument;
  var docW = doc.width;
  var docH = doc.height;
  var al = doc.activeLayer;
  // bounds is an array of [left,top,right,bottom] 
  // coordinates from the top left corner
  var bounds = al.bounds; 
  
  // distances from each side of the document
  var left = bounds[0];
  var right = docW - bounds[2];
  var top = bounds[1];
  var bottom = docH - bounds[3];

  // values to resize to. if left is more than right,// then use the smaller value multiplied by two,// say doc width is 400px,distance from right is 50px,from left is 150px:
  // crop to 400-50*2 = 300px
  var resizeWidth = docW - (left > right ? right * 2: left * 2);
  var resizeHeight = docH - (top > bottom ? bottom  * 2: top * 2);

  doc.resizeCanvas(resizeWidth,resizeHeight)

  // restore original units
  preferences.rulerUnits = startRulerUnits

enter image description here

,

虽然上面的答案是你所需要的,(我不是想与之竞争)它可能对以后的参考有用,注意你可以修剪到最小的边界框,然后调整画布大小。>

//trim image to transparent width
app.activeDocument.trim(TrimType.TRANSPARENT,true,true);


// adjust canvas size
//app.activeDocument.resizeCanvas(WIDTH,HEIGHT,AnchorPosition.MIDDLECENTER);
// AnchorPosition can vary,depending on what you want