有没有办法检测具有相同边界矩形的2个不同选择的不同大小?
使用
app.activeDocument.selection.bounds
将返回包围选区的 矩形 的尺寸。下面的两个选择将返回相同的 bounds
(39px x 38px)。我如何以编程方式区分它们?
当您选择的图像已转换为黑白位图时:
所以诀窍是获取每个像素的颜色样本。我们并不真正关心像素的颜色,而是它是否是选择的一部分。有的话就黑,没有就白。
然后就是循环整个图像,边走边数。
// creates an image of text that looks like
// a punch number on the back of a SNES cart
// The number will be random between 9 and 25;
// Switch off any dialog boxes
displayDialogs = DialogModes.ALL; // OFF
// call the source document
var srcDoc = app.activeDocument;
var imageW = 39; // Custom image width
var imageH = 38; // Custom image height
var count = 0;
var pixelCount = 0;
var imageArea = imageW * imageH;
delete_all_colour_samples(); //just to be safe!
alert("This may take a while!\nBe patient.")
for (var x = 0; x < imageW; x++)
{
for (var y = 0; y < imageH; y++)
{
try
{
var pointSample = srcDoc.colorSamplers.add([x,y]);
count += 1;
}
catch(eek)
{
alert("eek!\n" + "x,y\n" + x + ", " + y);
}
// Obtain RGB values.
var r = Math.round(pointSample.color.rgb.red,0);
var g = Math.round(pointSample.color.rgb.green,0);
var b = Math.round(pointSample.color.rgb.blue,0);
{
// if it's a selection pixel (black) count it!
if (r == 0 && g == 0 && b ==0) pixelCount +=1;
}
if (count >9)
{
// Photoshop can only have 10 samples at any time
delete_all_colour_samples();
// reset teh counter
count = 0;
}
}
}
var selectionArea = pixelCount;
alert("Selection is: " + selectionArea + "\n out of:" + imageArea);
// 1,111/1482
// 813 /1482
// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL
// function DELETE ALL COLOUR SAMPLES()
// --------------------------------------------------------
function delete_all_colour_samples()
{
// Kill all colour samples
app.activeDocument.colorSamplers.removeAll();
}