从透明像素中获取颜色样本



我正在尝试检测像素是否完全透明。更具体地说,图像是否在透明背景上。我以为

// Add a Color Sampler at a given x and y coordinate in the image.
var pointSample = app.activeDocument.colorSamplers.add([(x),(y)]);
// Obtain array of RGB values.
var rgb = [
pointSample.color.rgb.red,
pointSample.color.rgb.green,
pointSample.color.rgb.blue
];

会起作用的,而且确实。。。

除非像素具有0%的不透明度(即100%透明(,否则Photoshop会抛出General 8800 error。我会使用上面的try catch,但这可能不是实现它的方法

有什么想法吗?

我找到了一种确定图像是否透明的方法:

delete_all_colour_samples();
var isImageTransparent = has_transparency();
var x = 0;
var y = 0;
alert(isImageTransparent ? "Image has transparency." : "Image is opaque.");
if (isImageTransparent)
{
// do stuff
}
else
{
// get colour sample
// Add a Color Sampler at a given x and y coordinate in the image.
var pointSample = app.activeDocument.colorSamplers.add([(x),(y)]);
// Obtain array of RGB values.
var rgb = [
pointSample.color.rgb.red,
pointSample.color.rgb.green,
pointSample.color.rgb.blue
];
}

// function to see if image has transparency
function has_transparency() 
{
var desc52 = new ActionDescriptor();
var ref47 = new ActionReference();
ref47.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
desc52.putReference( charIDToTypeID('null'), ref47 );
var ref48 = new ActionReference();
ref48.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Trsp') );
desc52.putReference( charIDToTypeID('T   '), ref48 );
desc52.putBoolean( charIDToTypeID('Invr'), true );
try
{
executeAction( charIDToTypeID('setd'), desc52, DialogModes.NO );
}
catch(eek)
{
return false;
}
activeDocument.selection.deselect();
return true;
}

function delete_all_colour_samples()
{
// Kill all colour samples
app.activeDocument.colorSamplers.removeAll();
}

最新更新