Save/Export values from Eyedropper tool

Community Beginner ,
Mar 28, 2013 Mar 28, 2013

Copy link to clipboard

Copied

Hi @ll!

For my work, I'm measuring the color of objects which were photographed. I import the photograph, conduct a white balance and afterwards convert it to Lab.

Subsequently I'm using the Eyedropper tool to pick 12 separate points (averaged in 5x5 px) in the photograph in order to derive their Lab values.

Until now I have just written down the values manually. However, when more than 1000 images have to be worked at in the same way, I would like this process to be a bit easier.

So - is there any possibility for a script to automatically save the values from the eyedropper tool in some file (txt, csv, xls...)?

Thanks for your help!

Bob

TOPICS
Actions and scripting

Views

2.4K

Likes

Translate

Translate

Report

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
Valorous Hero ,
Mar 28, 2013 Mar 28, 2013

Copy link to clipboard

Copied

I think you mean you are using colorsampler.

Yes it is possible, but doing a couple of test it seems that the results are slightly different using a script to the values in Photoshop.

Using a script you have to select the area run the average filter then take the reading.

If you want to give it a try...

Save the script.

#target Photoshop

app.bringToFront();

var doc = decodeURI(activeDocument.name);

var Name = activeDocument.name.match(/(.*)\.[^\.]+$/)[1];

var outFile = File(Folder.desktop + "/colourSamples.csv");

var mySampler = app.activeDocument.colorSamplers[0];

var aColour = getAverageColor(1,5)

var result = aColour;

var L = result.lab.l;

var A = result.lab.a;

var B = result.lab.b;

var xy =mySampler.position;

var line =Name + ","+Math.floor(xy[0].as('px')) + ","+Math.floor(xy[1].as('px')) +",";

line += L.toFixed(2)+","+A.toFixed(2)+","+B.toFixed(2);

outFile.open('e');

outFile.seek (0, 2);

outFile.writeln (line);

outFile.close();

app.activeDocument.colorSamplers.removeAll();

function getAverageColor(s,A){

var doc = activeDocument;

        if (!s) { s=1;}

        if (!A) {A=1;}

        CP=doc.colorSamplers;

        s=s-1;

        sampleSize=A;

        r=((A-1)/2);

        x=Math.round(CP.position[0]-r);

        y=Math.round(CP.position[1]-r);

        doc.selection.select([[x, y], [x+sampleSize, y], [x+sampleSize, y+sampleSize], [x, y+sampleSize]], SelectionType.REPLACE, 0, false);

        doc.activeLayer.applyAverage();

        var sColor = new SolidColor();

        sColor=CP.color;

        executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );

        doc.selection.deselect();

        return sColor;

}

Set up, File - Scripts - Script Events Manager

Tick the checkbox: Enable Events to Run Scripts/Actions

Photoshop Event: select "Add an Event"

Enter:-

ColourSampler.jpg

N.B. Mk is Mk space space

ClSm is upercase C, lowercase L, upercase S, lowercase m

Once created,

Photoshop Event: select "Colour Sampler"

Script : select browse and select the saved script

Then click the Add button

Once set up every time you use the Color Sampler it will write :

FileName,X,Y,L,a,b to a csv file on the desktop called "colourSamples.csv"

good luck.

Likes

Translate

Translate

Report

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
Guru ,
Mar 28, 2013 Mar 28, 2013

Copy link to clipboard

Copied

Paul, I am not seeing the need for the average blur filter when I test colorSamplers. It is true that the values are slightly different than what appears in the info panel but that is just because the info panel is showing the values rounded to intergers.

Likes

Translate

Translate

Report

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
Valorous Hero ,
Mar 28, 2013 Mar 28, 2013

Copy link to clipboard

Copied

The values were upto 3 out when doing a 5 pixels average but are spot on when a single pixel has been selected.

Mike are you saying the color sample will return the colour average of what is selected in Photoshop?

Likes

Translate

Translate

Report

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
Guru ,
Mar 28, 2013 Mar 28, 2013

Copy link to clipboard

Copied

The eyedropper tool and colorSampler tool share the Sample Size option in the options bar. With that option set to 5x5 and the image in Lab mode I get the same values as shown in the info panel except for the rounding. I am not sure if a script can set the Sample Size option so that may need to be set before the script runs. A quick look in my code library shows I have a function to get the current sample size but not one to set it.

If the image is not in Lab mode then the values due differ more. I think that has more to do with the current color settings and render intent.

Also it is with newer versions of Photoshop. I remember a long thread in the old Adobe Photoshop Scripting forum about Lab color and the info panel between Chris Cox and Rags Gardner. I think Rags finally got his point across and the next version of Photoshop shows the expected values. I'm guessing that was around CS2 or CS3 but may be older.

Likes

Translate

Translate

Report

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
Valorous Hero ,
Mar 28, 2013 Mar 28, 2013

Copy link to clipboard

Copied

That does explain a lot of things Mike, I was only using the colorSampler.

I suppose that using just the eyedropper could be awkward as this would be used for uses than the colorSampler and that would mean you would need to turn off that event when not measuring.

Likes

Translate

Translate

Report

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
Valorous Hero ,
Mar 28, 2013 Mar 28, 2013

Copy link to clipboard

Copied

That would simplifly the script quite a lot Mike.

The script would then be...

#target Photoshop

app.bringToFront();

var doc = decodeURI(activeDocument.name);

var Name = activeDocument.name.match(/(.*)\.[^\.]+$/)[1];

var outFile = File(Folder.desktop + "/colourSamples.csv");

var result = app.foregroundColor;

var L = result.lab.l;

var A = result.lab.a;

var B = result.lab.b;

var line =Name + ","+ Math.ceil(L)+","+Math.ceil(A)+","+Math.ceil(B);

outFile.open('e');

outFile.seek (0, 2);

outFile.writeln (line);

outFile.close();

To set up Script Events Manager, would be

The name you want to give it

Events:-

setd,Clr

The Clr is Uppercase C, lowercase L, lowercase R and a space (must be 4 chars)

Likes

Translate

Translate

Report

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
Guru ,
Mar 28, 2013 Mar 28, 2013

Copy link to clipboard

Copied

I would recommend turning off the event handier when not needed no matter what event is being monitored.

Depending on the requirements there may be a better approach than using event handlers. Are the areas to be sampled at the same location for each document. If so you could have a standalone script that gets the colors and writes the data file in one pass.

If the areas to be sampled vary from image to image you could set the areas to be sampled using the pen tool with each point being a sample spot. A script could then get the pathPoints and again sample all the areas and create the data file in one pass.

Guess it mainly depends on if the op wants one data file with all the samples or one data file per image and if a full set of 12 samples was important. The event handler approach would more easily allow for more or fewer samples per image. The other approaches could alert an message if there were not 12 sample areas and insure there is a full data set per image.

Likes

Translate

Translate

Report

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 Beginner ,
Mar 29, 2013 Mar 29, 2013

Copy link to clipboard

Copied

Thanks Paul, I tested your script. I seem to have some issues - 1. no file is created or edited on the Desktop (I'm using Wndows 7 and CS5.1), 2. I have to "clear" the points every 4 selections.

Michael, I want to measure the same region for each object but the position of each object is not the same between images. Furthermore, all points (colors) are surrounded by black borders which makes exact sampling per image important. So there is no way to conduct a "batch" operation I guess I have to do that manually.

I prefer it to be one file for all images because I have to use the values for statistical purposes.

Furthermore, I explicitly said the "eyedropper" tool instead of the "color sampler" because the color sampler is limited to 4 points and I have to tediously "delete" the points three times per image (remember, I need 12 points).

Thanks everyone,

Bob

Likes

Translate

Translate

Report

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
Valorous Hero ,
Mar 29, 2013 Mar 29, 2013

Copy link to clipboard

Copied

Ok, there could be several problems.

Windows 7 is known for not refreshing the screen, try right click on the desktop and select refresh.

Check that the events have been entered correctly by removing the entry in Script Event Manager and set it up so that it uses the Welcome script, then when you use the eyedropper you should get an alert.

If there is no file and the welcome alert works, the problem could be how you have saved the script. It needs to be saved as a plain text file with an extension of .jsx a lot of editors will add control characters.

Using the ColorSampler would not have been a problem as if you looked at the code it removes the sampler automatically.

Likes

Translate

Translate

Report

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 Beginner ,
Mar 29, 2013 Mar 29, 2013

Copy link to clipboard

Copied

By testing the "Welcome" alert - and it did not work - I found out that i forgot to add a space after the event manager command "Clr" --> "Clr " and it worked

I guess that works fine. So, unless someone has a much better idea, I will be using your solution, Paul.

Thanks for the script

Bob

Likes

Translate

Translate

Report

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 ,
Nov 01, 2015 Nov 01, 2015

Copy link to clipboard

Copied

Hi Paul,

I checked almost all the historical discussion and came upon this discussion that you made in 2013.

I'm trying to get to something similar, which is to record the colorsampler's output, but in a more selective manner.

Is it possible to have something like this? The process should be something like:

1. Launch of the script with an active dialogue box

2. Choose the desired Sample Size

3. The script "activates" the Colorsampler tool

4. User click on 5 (i wanted 5 instead of the 4 as shown in the image) location of the background image

5. Click Start and the 5x LAB is recorded in a .csv file on desktop.

Screen Shot 2015-11-01 at 8.49.54 pm.png

I'm quite a newbie in scripting in photoshop but i suppose this script would be useful to many after me. Hope you can point me towards the correct direction. thank you so much!

Likes

Translate

Translate

Report

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 ,
Jul 27, 2022 Jul 27, 2022

Copy link to clipboard

Copied

LATEST

hi Michael 🙂
you wrote "A quick look in my code library shows I have a function to get the current sample size but not one to set it."

Does it mean that if I will set the color sampler to 5 by 5 before running the script then it will truly use the average color around the exact point which I am indicating in the script? I am a beginner, my script works fine with taking the sample from exact pixel but I need to change it to sample size 5 by 5 so the background color which it is making will be more accurate.
I know I am refreshing very old discussion so any answear will be even more and more appreciated 🙂

My very complicated version of course is:

var docRef = app.activeDocument;
var pixelLoc = [10,10];
var colorSamplerRef = docRef.colorSamplers.add(pixelLoc);
app.foregroundColor = colorSamplerRef.color;

Likes

Translate

Translate

Report

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