Skip to main content
DigDigDig
Participating Frequently
January 20, 2015
Answered

Does anyone know of a script clear the histogram cache and copy its data to the clipboard?

  • January 20, 2015
  • 5 replies
  • 1154 views

I find myself manually copying pixel counts from my histogram into excel and am wondering if there is an existing script that allows the user to copy the histogram data onto the clipboard. 

From a QA standpoint alone this would be a huge advantage! 

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Does this work for you – you only want the total number of the pixels in the Selection in the Clipboard, right?

// 2015, use it at your own risk;

#target "photoshop-70.032"

if (app.documents.length > 0) {

  var myDocument = app.activeDocument;

  var theName = myDocument.name;

  try {var theName = theName.match(/(.*)\.[^\.]+$/)[1]} catch (e) {};

  try {var thePath = myDocument.path}

  catch (e) {var thePath = "~/Desktop"};

  if (myDocument.mode == DocumentMode.MULTICHANNEL || myDocument.mode == DocumentMode.GRAYSCALE) {

  var theHistogram = myDocument.channels[0].histogram

  };

  else {var theHistogram = myDocument.histogram};

// get total number of histogram:

  var theNumber = 0;

  for (var m = 0; m < theHistogram.length; m++) {theNumber = theNumber + theHistogram};

  copyTextToClipboard (theNumber);

/* var theText = new File(thePath+"/"+theName+"_histo.txt");

  theText.open("w");

  for (a = 0; a < theHistogram.length; a++){

// this line adds the number of the histogram-level;

  theText.write(bufferNumberWithZeros(a,3)+"_");

// this line adds the number pixels of the respective histogram-level;

  theText.write(theHistogram);

  theText.write("\n")

  }

  theText.close();*/

  }

else {

  alert ("no open document")

  };

////// buffer number with zeros //////

function bufferNumberWithZeros (number, places) {

  var theNumberString = String(number);

  for (var o = 0; o < (places - String(number).length); o++) {

  theNumberString = String("0" + theNumberString)

  };

  return theNumberString

  };

//////

function copyTextToClipboard( txt ) {

const keyTextData = app.charIDToTypeID('TxtD');

const ktextToClipboardStr = app.stringIDToTypeID( "textToClipboard" );

var textStrDesc = new ActionDescriptor();

textStrDesc.putString( keyTextData, txt );

executeAction( ktextToClipboardStr, textStrDesc, DialogModes.NO );

};

5 replies

DigDigDig
DigDigDigAuthor
Participating Frequently
January 21, 2015

I found this posting on Stack Overflow.

Photoshop 13.1 (the latest Creative Cloud release of Photoshop CS6) now has a hook allowing you to do this directly. Here's a sample function:

function copyTextToClipboard( txt )

{

     const keyTextData = app.charIDToTypeID('TxtD');

     const ktextToClipboardStr = app.stringIDToTypeID( "textToClipboard" );

     var textStrDesc = new ActionDescriptor();

     textStrDesc.putString( keyTextData, txt );

     executeAction( ktextToClipboardStr, textStrDesc, DialogModes.NO );

}

Please note this won't work in versions of Photoshop prior to 13.1

I haven't had any luck implementing it though.

c.pfaffenbichler
Community Expert
Community Expert
January 21, 2015

What do you know, it can be done non-platform dependent after all it seems … works for me.

What is the complete code wherein it fails for you?

DigDigDig
DigDigDigAuthor
Participating Frequently
January 21, 2015

I'm on Windows 8. 

I've played around with the histogram.jsx that was included in Photoshop CS4(?). 

It receives its total pixel count from imageH * imageW so it only works on rectangular selections.

My specific application involves creating a new layer from an irregular shape selected in an image and then measuring the total pixels present in that shape which are then copied into an excel spreadsheet.  We have a constant that we multiply our pixel count by in order to determine the area in centimeters of our irregular selection.  For example if our layer contains 1109078px and we know that our images scales at 255.53 pixels/cm then we can calculate our selected area to be 21.84 sq cm.


Because we make measurements twice on each of the hundreds of images spanning dozens of sites I end up copying the pixel count from the histogram by hand an huge number of times.  Hand copying means the possibility for transcription error which has the potential to alter our data.  Just to give you a baseline, I've made 733 transcriptions of that readout on the current site I'm analyzing. 

Getting at the histogram and copying directly to the clipboard would be the most efficient and sound way to copy the data.

c.pfaffenbichler
Community Expert
Community Expert
January 21, 2015
Getting at the histogram and copying directly to the clipboard would be the most efficient and sound way to copy the data.

Are you sure?

A Script could handle many files, so one could store the Selection as a Channel for a bunch of them, have the Script work through that, do the math and then create a txt file listing the results per image.

And are you familiar with Measurement Log?

c.pfaffenbichler
Community Expert
Community Expert
January 21, 2015

Histogram Export

How to get text into the clipboard would probably have to be platform dependent.

What is your OS?

JJMack
Community Expert
Community Expert
January 21, 2015

I think you would need to use some system screen capture feature like Prtscrn to get the histogram into the clipboard and that it would be an image and not text data.  I do not see any interface in the histogram palette to copy histogram data to the clipboard.

However if you look at pager 74 in the Photoshop javascript reference guide you will fnd that you can get at the histogram data  and write a text file.  There is an example script for doing that there.....

Page 74 Channels sample script

The following script opens a file if one is not already open, and then writes a histogram report

(histogram.log) for the channels in the active document.

Note: This script contains a switch construction that uses a break statement. The break statement

requires an ending semicolon (;), as in the following sample:

break;

Histogram.jsx

JJMack
c.pfaffenbichler
Community Expert
Community Expert
January 21, 2015

Why involve the clipboard at all, might not a txt file be more convenient?

What are you doing exactly anyway?