Copy link to clipboard
Copied
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!
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 ==
Copy link to clipboard
Copied
Why involve the clipboard at all, might not a txt file be more convenient?
What are you doing exactly anyway?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
How to get text into the clipboard would probably have to be platform dependent.
What is your OS?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
I'm on Windows 8.
I’m a Mac user, so I don’t know how to pass a string to the Clipboard on Windows.
Have you down a Forum search for that yet? (Maybe also try http://ps-scripts.com/bb/)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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 );
};
Copy link to clipboard
Copied
Well that certainly did the trick! I was going about it a little differently and only able to copy a rectangular boundary around my layer. This works perfectly.
Copy link to clipboard
Copied
Better do a few tests before you start trusting that code, though.
Feathering and soft edges of the Selection would affect the result, so that may deserve some attention spending on the way you create the Seletcions.
Copy link to clipboard
Copied
We arrive at one of our measurable layers through a Color Range selection which does create a soft edge. The diffuse portion of that selection is usually considered an artifact of our photography process so dropping a few pixels in that region is really not a problem.
I've been running a some tests with it and so far any discrepancy between the manual entry and the script seem pretty minimal, accounting for ~.1mm average difference.
Thanks again for the assistance here. I can't even tell you how much more efficient this has been so far!
Copy link to clipboard
Copied
Maybe you could edit the Selection (in Quick Mask mode for example) with Threshold or Levels to include the semi-transparent pixels.
Quick Mask Mode might offer a different approach in that one could assess the Histogram of that channel itself.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now