Skip to main content
Participant
July 5, 2024
Question

Script JS for PS : How to change the source of histogram ?

  • July 5, 2024
  • 3 replies
  • 349 views

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 🙂

3 replies

Participant
February 27, 2025

what do you use to navigate the data structure of the activeDocument in your screenshot?

Stephen Marsh
Community Expert
Community Expert
July 5, 2024

@PaulineLHG wrote:

 

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 😞


 

Usually, one would dupe the active layer to a new temp doc, check the histogram and then close the temp doc without saving.

 

I can't remember how scripting histograms works with selections.

 

As @c.pfaffenbichler wrote, if you know where to correctly place a color sampler it may be better to use that.

c.pfaffenbichler
Community Expert
Community Expert
July 5, 2024

I think creating a Selection and a new Layer takes time that could be avoided. 

c.pfaffenbichler
Community Expert
Community Expert
July 5, 2024

Why don’t you use the Color Picker instead of creating a new Layer? 

And might you be mixing ESTK and UXP code? That would not work out well. 

 

 

// 2024, use it at your own risk;
if (app.documents.length > 0) {
app.activeDocument.colorSamplers.removeAll();
var theSampler = app.activeDocument.colorSamplers.add([1000,1000]);
alert (Math.round(theSampler.color.rgb.red) + "," + Math.round(theSampler.color.rgb.green) + "," + Math.round(theSampler.color.rgb.blue) + ";");
};