How to get vaules in the histogram

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

pzwhz

How to get vaules in the histogram

Post by pzwhz »

How to get values in the histogram from a script?
For example: Mean,Std Dev,Median,Pixels.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

How to get vaules in the histogram

Post by pfaffenbichler »

With mathematic, I guess.
At least I don’t know another way to access that information.

Maybe this helps somewhat:
Code: Select all// 2012, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theHist = myDocument.histogram;
// get total number;
var thePixels = 0;
for (var m = 0; m < theHist.length; m++) {
   thePixels = thePixels + theHist[m]
   };
// get mean and median;
var theMean = 0;
var aTotal = 0;
var check = false;
for (var n = 0; n < theHist.length; n++) {
   theMean = theMean + (n * theHist[n] / thePixels);
   aTotal = aTotal + theHist[n];
   if (aTotal >= thePixels / 2 && check == false) {
      theMedian = n;
      check = true;
      }
   };
alert (thePixels + " pixels\n" + theMean + " mean\n" + theMedian + " median")
};
pzwhz

How to get vaules in the histogram

Post by pzwhz »

These code is to get the Histogram of whole document , how to get a layer's histograms? Like photoshop can select whole document or a layer selected .
pfaffenbichler

How to get vaules in the histogram

Post by pfaffenbichler »

The Document Object Model does not seem to provide a histogram-property for (art)layers.
So short of hiding all other layers and loading the layer’s transparency as a selection I can’t think of a (reasonable) method to get that.
Maybe one of the regulars knows more about it, though.

What is your ultimate target for this information anyway?
xbytor

How to get vaules in the histogram

Post by xbytor »

Code: Select allSo short of hiding all other layers and loading the layer’s transparency as a selection I can’t think of a (reasonable) method to get that.

This is true. Also, the histogram is shallow, only 8 bits instead of 16 or 32. You are getting a rough approximation using Document.histogram regardless of what you do. There is a file in xtools that handles some of the maths needed but I've never figured a way to get exactly the numbers that show up in the histogram palette.
pzwhz

How to get vaules in the histogram

Post by pzwhz »

There are many artlayers in the document.
I want to get the histogram of a artlayer by name.
If the artlayer has transparent pixels, the best not statistics the transparent pixels.
Can you give me the script?
pfaffenbichler

How to get vaules in the histogram

Post by pfaffenbichler »

As xbytor has confirmed that a viable approach would be to hide the other Layers and load the transparency you could just try amending the Script accordingly yourself.