8 Replies Latest reply on Sep 29, 2010 9:14 PM by Michael L Hale

    How to get, for a serie of greyscale images, the absolute min and max levels values ?

    Alesque

      Hello,

       

      I apologize in advance for my english, I am a french PhD student.

       

      I have a few series of four images in grayscale. For a given serie, the min and max levels are not the same for the four images. For instance, we could say that for

      image1, it is min=2 and max=188

      image2, it is min=36 and max=99

      image1, it is min=123 and max=239

      image1, it is min=45 and max=167

       

      I would like to write a script that :

      - finds, among these 4 images, the absolute minimum (2 in the previous example) and maximum (239)

      - modifies the output levels of these images with these values

       

      In javascript, I've found the function "adjustLevels" with "outputRangeStart" and "outputRangeEnd" that could help for the second step, but I don't know how to get the min and max values.

       

      Thank you.

       

      Alex

        • 1. Re: How to get, for a serie of greyscale images, the absolute min and max levels values ?
          Muppet Mark-QAl63s Level 4

          Do forgive me if this is totally wrong I don't do this type of stuff often…

           

          #target photoshop
          
          if (documents.length > 1) {
               var min = 255;
               var max = 0;
               for (var a = 0; a < documents.length; a++) {
                    app.activeDocument = documents[a];
                    var x = app.activeDocument.channels[0].histogram;
                    for (var b = 0; b < x.length; b++) {
                         if (x[b] != 0) {
                              min = Math.min(min,b);
                              $.writeln(b);
                              break;
                         }
                    }
                    for (var c = x.length-1; c > 0; c--) {
                         if (x[c] != 0) {
                              max = Math.max(max,c);
                              $.writeln(c);
                              break;
                         }
                    }
               }     
               $.writeln(min);
               $.writeln(max);
               for (var d = 0; d < documents.length; d++) {
                    app.activeDocument = documents[d];
                    app.activeDocument.activeLayer.adjustLevels(0,255,1.0,min,max);
               }
          }else{
               alert("NOT enough docs to compare?");
          }
          

          • 2. Re: How to get, for a serie of greyscale images, the absolute min and max levels values ?
            Michael L Hale Level 5

            I am also not sure this is what you want, but if you are trying to adjust the four image so they all have the same black and white point( 2 and 239 in your example ) I think you need to set the input start and end to the current docs values and set the target start and end to the target values. Something like this.

            // assumes 4 greyscale images opened in Photoshop
            var min = 255;
            var max = 0;
            for( d=0;d<app.documents.length;d++){
                app.activeDocument = app.documents[d];
                var endPoints = getEndPoints();
                min = Math.min(min,endPoints[0]);
                max = Math.max(max,endPoints[1]);
            }
            for (var d = 0; d < documents.length; d++) {
                app.activeDocument = documents[d];
                var endPoints = getEndPoints();
                app.activeDocument.activeLayer.adjustLevels(endPoints[0],endPoints[1],1.0,min,max);
            }
            function getEndPoints(){
                var a = app.activeDocument.channels[0].histogram;// copy the histogram to an array
                var re = RegExp( '[1-9][0-9]*' );
                var blackPoint = re.exec(a.toString() ).index/2; // the index of the lowest element with non-zero value
                a.reverse();// flip the array to check white point with RegExp
                var whitePoint = 255-(re.exec(a.toString() ).index/2);// the index of the hightest element with a non-zero value
                return [ blackPoint, whitePoint ];
            }
            
            1 person found this helpful
            • 3. Re: How to get, for a serie of greyscale images, the absolute min and max levels values ?
              Alesque Level 1

              Hello !

               

              Thank you Muppet Mark and Michael L Hale !

               

              Muppet Mark, I've tried your script : it works.

               

              Michael L Hale, I haven't tried yours yet, but I will soon.

               

              Thank you again.

               

              Alex

              • 4. Re: How to get, for a serie of greyscale images, the absolute min and max levels values ?
                Muppet Mark-QAl63s Level 4

                I have NOT had the chance to run Mike's but it looks like it could be the same functionality… Just a better use of syntax… And as usual a lesson to me… Flip the array with reverse() never even entered my head… oh well…

                • 5. Re: How to get, for a serie of greyscale images, the absolute min and max levels values ?
                  Michael L Hale Level 5

                  Mark, I think the difference is more that just syntax. Getting the white and black points could be done either way. I think searching with an RegExp is faster but looping the histogram works as well and is a more common approach.

                   

                  My code adjusts the images differently. I don't know which would be better, it depends on the results required by the OP. Mark's script sets the input values to the default 0,255 and the output vales to min, max. Mine sets the input values to the original image black/white point and the output to min,max. That difference effects the final black and white points of the adjusted image.

                   

                  I posted it because, from reading what the OP posted, I thought it made the required adjustments. As he is happy with your version I guess I misread what he wanted done.

                  1 person found this helpful
                  • 6. Re: How to get, for a serie of greyscale images, the absolute min and max levels values ?
                    Alesque Level 1

                    Hello,

                     

                    No, you're wright Michael, it is preferable for the input values to be the original image black/white point. I didn't notice that it wasn't the case with Mark's code.

                     

                    Thank you again !

                    • 7. Re: How to get, for a serie of greyscale images, the absolute min and max levels values ?
                      bburns66

                      First post ever on Adobe forums....

                       

                      With the script above is it possible to use this on color images as well?   Also, can someone tell me what tools are used to edit scripts in Photoshop??

                       

                      bburns66

                      • 8. Re: How to get, for a serie of greyscale images, the absolute min and max levels values ?
                        Michael L Hale Level 5

                        I would think that if you changed

                         

                        var a = app.activeDocument.channels[0].histogram;

                        To

                        var a = app.activeDocument.histogram;

                        it should work with rgb, cmyk, or lab images.