Script JS for PS : How to change the source of histogram ?
Hi everyone 🙂
We are writting a script on ExtendToolKit for Photoshop and we need your help !
Context :
We have a e-commerce website and we are changing our format of our product images.
Currently, it is all in square and we are changing it in rectangle. Our problem is that all our images have been crop manually for years (15y). So we can't just crop them in right and left side because for some of them it will cut the product and we can't just add blank on the top and the bottom because some product will be far to small.
What we are planning to do :
We are writing a script to check in two areas if there are colored pixels. For that, we defined an area, copied it in a new layer and we would like to check the histogram of these layer to see if there are colored pixel. We make a loop. If not for both areas, we crop the img, if there are, we just close the doc and we will do another script later.
Our problem :
We have issues to access to the propriety "histogram" of the activeLayer and not activeDocument. Do you have any idea how to change the source of the histogram ? On the interface it's really easy but with the code it's a mystery for us 😞

function zoneContainsColor(zone) {
var hasColor = false;
// select the area
doc.selection.select([
[zone.x, zone.y],
[zone.x + zone.width, zone.y],
[zone.x + zone.width, zone.y + zone.height],
[zone.x, zone.y + zone.height],
]);
// pasted the area in a new layer
var layer = doc.artLayers.add();
doc.paste();
var tempLayer = doc.activeLayer;
// Get pixels data by histogram
var pixelData = doc.histogram;
for (var i = 0; i < pixelData.length; i++) {
if (pixelData[i] > 0) {
hasColor = true;
break;
}
}
// delete temp layer
tempLayer.remove();
return hasColor;
} Thank you for your help !
Have a nice day 🙂
