Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Jul 05, 2024 Jul 05, 2024

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 😞

UI27907670zinp_0-1720176914188.png

 

 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 🙂

TOPICS
Actions and scripting , Windows
366
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Jul 05, 2024 Jul 05, 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) + ";");
};

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2024 Jul 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2024 Jul 05, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 27, 2025 Feb 27, 2025
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines