简体   繁体   中英

Get painted area dimensions of a layer through Photoshop Javascript

How can I check Painted dimensions (not the canvas or image size) of a layer through Photoshop Javascript? I have tried to find this property in Photoshop Javascript manual / reference guide without any luck so far. Below is an image just for your reference which gives you painted area dimensions (height and width wise) but somehow I want to get this information through script for a certain action that has to be taken later on once this information is fetched from an image. Thanks!

像素图层属性

You can use the ArtLayer bounds property to get an array containing the coordinates of the bounding box, then use those values to generate the width and height as displayed by the Layer Properties palette. As an example, I have the following document and the Properties palette is showing the width and height is 4.17 in.

Using the following code, I can capture the coordinates as x1, y1, x2, and y2, then subtract x1 from x2 and y1 from y2 to get the width and height to match the output of the Properties palette:

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM