通过Photoshop Javascript获取图层的绘制区域尺寸

时间:2018-04-04 18:02:10

标签: javascript javascript-objects photoshop-script

如何通过Photoshop Javascript检查图层的绘制尺寸(不是画布或图像尺寸)?到目前为止,我试图在Photoshop Javascript手册/参考指南中找到这个属性,但没有任何运气。下面的图片仅供您参考,它为您提供了绘制的区域尺寸(高度和宽度)但不知何故,我想通过脚本获取此信息,以便稍后在从图像中获取此信息后必须采取该操作。谢谢!

Pixel Layer Properties

2 个答案:

答案 0 :(得分:2)

您可以使用ArtLayer bounds属性获取包含边界框坐标的数组,然后使用这些值生成“图层属性”调板显示的宽度和高度。例如,我有以下文档,“属性”面板显示的宽度和高度为4.17英寸。

使用以下代码,我可以将坐标捕获为x1,y1,x2和y2,然后从x2减去x1,从y2减去y1以获取宽度和高度,以匹配“特性”选项板的输出:

var activeDoc = app.activeDocument;
var layerBounds = activeDoc.activeLayer.bounds;
// this particular document, activeLayer.bounds returns:
// 0.41666666666667 in, 0.41666666666667 in, 4.5833333333333 in, 4.5833333333333 in
// x1, y1, x2, y2

// we specify the vlaue property so we only get the number without the ruler unit
var x1 = layerBounds[0].value;
var x2 = layerBounds[2].value;
var y1 = layerBounds[1].value;
var y2 = layerBounds[3].value;

// finally subtract x1 from x2 and y1 from y2 to get the widht and height and 
// fix the size to 2 decimal units to match the Properties palette
var layerPaintedWidth = (x2 - x1).toFixed(2); 
var layerPaintedHeight = (y2 - y1).toFixed(2);

// display the results in an alert dialog as 'W = 4.17, H = 4.17'
alert("W = " + layerPaintedWidth + ", H = " + layerPaintedHeight);

答案 1 :(得分:1)

设置选择(例如:命令 + 选择图层),然后在跟踪选择尺寸的信息面板中填充宽度和高度值。