6 Replies Latest reply on Jun 10, 2013 5:10 PM by fred_lm

    [Help needed] Sample colour in each cell of a grid


      Hi All,


      First of all, HAPPY OCEANS DAY!


      Here is my problem:


      I have hundreds of maps of exactly the same size, for which I would need to extract the "value of the colour" in each cell of each map.

      There is no way I can get my hands on the GIS data, hence the need to use Photoshop.

      Here is a example of these maps:

      Screen Shot 2013-06-08 at 1.25.40 PM.png


      I hope it is possible to generate a script to super-impose a grid on that map, and then get the colour in each cell. Then I'd just need to convert the colour quote into actual value, and I'm done (but there is not need to use PS for that). I did some research on the internet, but didn't find anything satisfying (I may have not used the right keywords).


      Do you think this is a realistic option, or should I just give up right away?


      If the answer is "yes, it might be possible", then I might also need your help to write the script



      Thanks a lot for your help!



        • 1. Re: [Help needed] Sample colour in each cell of a grid
          Michael L Hale Level 5

          You said the images are all the same size. Is the grid the same size and offset on all the images?. Are they all tagged with a color profile and if so do they all use the same profile?

          • 2. Re: [Help needed] Sample colour in each cell of a grid
            FishFred Level 1

            Hi Michael,


            Thanks for your reply.

            Yes, the grid and will have the exact same size/offseet on all images, and the colour profile will also always be the same.

            Essentially, all images are the same, except that the color of each cell will be different (but always one of 5 shades of red)

            • 3. Re: [Help needed] Sample colour in each cell of a grid
              Michael L Hale Level 5

              In that case I think I would create an array of x,y positions for the center of each grid. That array will be used to set the position of a colorSampler in all the maps. For each map the script creates a new colorSampler then in a loop moves the sampler to a position in the array, get the color and write it to a data file. Would you want it to only log shades of red( skip white areas in the grid )?


              I can help you write the script but it would be helpful to have a sample file. Can you post a link?

              • 4. Re: [Help needed] Sample colour in each cell of a grid
                FishFred Level 1

                That would be tremendous!

                I just thought about another potential problem, though... some cells include (a bit of) landmass, which should not be sampled either. So rather than sampling the center of each grid, it might actually make more sense to get the average colour of ach cell, so that we still get the 'value of coastal cells'. Does it make sense?


                The maps will be generated from this website: http://www.fao.org/figis/geoserver/tunaatlas/, and you can find a sample at: http://i.imgur.com/Wn6ED0r.png (each file will be like that one).

                The color profile is:




                • 5. Re: [Help needed] Sample colour in each cell of a grid
                  Michael L Hale Level 5

                  I am not sure how helpful this script will be to you. The sample image is low res and not color managed. There is also a problem that the grid is anti-alias and not aligned to pixel grid.


                  However this will output the location and color of any reddish areas in the grid.


                  // wrap code in a function so it will only use one history state( single undo )
                  app.activeDocument.suspendHistory('Get colors', 'getColors()');
                  function getColors(){
                      var datFile = new File('~/Desktop/colorData.txt');
                      var startPosition = [282,275];// top left corner of first sample
                      var numberOfColumns = 106;
                      var numberOfRows = 35;
                      var sampleSize = 3;// i.e 3x3 pixel sample area
                      var cellOffset = 4;// number of pixel to next sample area from bottom right of current sample
                      var vertOffset = 0;// used to place sample in loop
                      var horzOffest = 0;
                      for( var r = 0; r < numberOfRows; r++ ){
                          for( var c = 0; c < numberOfColumns;c++ ){
                              var color = getColorOfPixel( startPosition[0]+horzOffest, startPosition[1]+vertOffset, sampleSize );
                              // uncommet to see sample placement
                              // app.activeDocument.selection.fill (app.foregroundColor);
                              if(color.lab.a > 5 && color.lab.b > 5 && color.lab.a >= color.lab.b  ){// if color is on red side of color wheel
                                  // do something with color
                                  datFile.writeln('R'+r+'C'+c+' hexValue: '+color.rgb.hexValue);
                              horzOffest = horzOffest + sampleSize + cellOffset;
                              // gird is not pixel aligned. this tries to adjust the placement to deal with that
                              // after 10 samples the offest is moved to the right 1 pixel
                              if(c>0 && c%10 == 0) horzOffest++;
                          horzOffest = 0;
                          vertOffset =  vertOffset + sampleSize + cellOffset;
                          // same pixel aligment problem
                          if(r>0 && r%10 == 0) vertOffset++;
                  function getColorOfPixel( varX, varY, sampleSize ) {
                      activeDocument.selection.select( [[varX, varY], [varX + sampleSize, varY], [varX + sampleSize, varY + sampleSize], [varX, varY + sampleSize]] );
                      var re = RegExp( '[123456789]' );
                      var sColor = new SolidColor();
                      sColor.rgb.red = re.exec(activeDocument.channels[0].histogram.toString() ).index/2;
                      sColor.rgb.green = re.exec( activeDocument.channels[1].histogram.toString() ).index/2;
                      sColor.rgb.blue = re.exec( activeDocument.channels[2].histogram.toString() ).index/2;    
                      return sColor ? sColor : undefined;
                  • 6. Re: [Help needed] Sample colour in each cell of a grid

                    Wow, thank you. That was quick!

                    The script works fine, so thank you very much.


                    I might get back to you later if I don't understand specific things.