8 Replies Latest reply on Oct 6, 2017 6:05 AM by c.pfaffenbichler

    Convert an image to an 'informative'color gradient


      Dear all,


      I have a rather strange question. I have a black/white photo. I want to 'extract' the information regarding the amount of black-gray-white (light and dark) within the photo, so I in the end I have a gradient map (like the one below) that shows the amount of black and white that the photo consist of.


      So if, for example, the image I want to convert is bw and very very dark with just some spots of light, the gradient map would mostly be black with just some gray and white-tones at the very end at the right side of the map. 


      Is this possible? I use Photoshop CC 2017


      I would be super thankful for answers.




        • 1. Re: Convert an image to an 'informative'color gradient
          davescm Adobe Community Professional



          The info such a gradient will show you is contained in the luminosity histogram - but I don't know a way to convert that to a cumulative gradient automatically in Photoshop


          Perhaps the scripting experts may be able to help.



          • 2. Re: Convert an image to an 'informative'color gradient
            Arvid.Stark Level 1

            Thank you for the fast answer Dave.


            I belive this is the way to go, i.e. to convert the histogram to a gradient. I just hope that someone know how to do this:)

            • 3. Re: Convert an image to an 'informative'color gradient
              c.pfaffenbichler Level 8

              You could evaluate the Histogram with a Script and then have the Script create a representation of that.

              I’m not sure a Gradient Layer would be the way to go, maybe create Shape Layers instead, but this would likely be far from speedy.

              Rounding issues might affect the result, though.

              • 5. Re: Convert an image to an 'informative'color gradient
                c.pfaffenbichler Level 8

                You could try this:


                // create rectangular shapes representing percentages of the (composite) histogram;

                // 2017, use it at your own risk;

                #target photoshop

                if (app.documents.length > 0) {

                var originalRulerUnits = app.preferences.rulerUnits;

                app.preferences.rulerUnits = Units.PIXELS;

                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"};

                var theTotal = myDocument.width * myDocument.height;

                // the values;

                var theArray = new Array;

                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++) {

                  var thisValue = theHistogram[m];


                  theNumber = theNumber + theHistogram[m]


                if (theNumber != theTotal) {alert ("something’s wrong")}

                else {};

                // create shape layers;

                var theX = myDocument.width;

                var theWidth = myDocument.width;

                var theBottom = myDocument.height;

                var theTotal = theWidth * theBottom;

                for (var n = 0; n < theArray.length; n++) {

                // only if non-zero;

                if (theArray[n] != 0) {

                var thePerc = theArray[n] / theTotal;

                rectangleShapeLayer ([0,0,theX,theBottom], n, n, n);

                theX = theX - (thePerc * theWidth);



                // reset;

                  app.preferences.rulerUnits = originalRulerUnits;


                else {

                  alert ("no open document")


                ////// rectangle //////

                function rectangleShapeLayer (theArray, theR, theG, theB) {

                // =======================================================

                var idMk = charIDToTypeID( "Mk  " );

                    var desc44 = new ActionDescriptor();

                    var idnull = charIDToTypeID( "null" );

                        var ref9 = new ActionReference();

                        var idcontentLayer = stringIDToTypeID( "contentLayer" );

                        ref9.putClass( idcontentLayer );

                    desc44.putReference( idnull, ref9 );

                    var idUsng = charIDToTypeID( "Usng" );

                        var desc45 = new ActionDescriptor();

                        var idType = charIDToTypeID( "Type" );

                            var desc46 = new ActionDescriptor();

                            var idClr = charIDToTypeID( "Clr " );

                                var desc47 = new ActionDescriptor();

                                var idRd = charIDToTypeID( "Rd  " );

                                desc47.putDouble( idRd, theR );

                                var idGrn = charIDToTypeID( "Grn " );

                                desc47.putDouble( idGrn, theG );

                                var idBl = charIDToTypeID( "Bl  " );

                                desc47.putDouble( idBl, theB );

                            var idRGBC = charIDToTypeID( "RGBC" );

                            desc46.putObject( idClr, idRGBC, desc47 );

                        var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );

                        desc45.putObject( idType, idsolidColorLayer, desc46 );

                        var idShp = charIDToTypeID( "Shp " );

                            var desc48 = new ActionDescriptor();

                            var idunitValueQuadVersion = stringIDToTypeID( "unitValueQuadVersion" );

                            desc48.putInteger( idunitValueQuadVersion, 1 );

                            var idTop = charIDToTypeID( "Top " );

                            var idPxl = charIDToTypeID( "#Pxl" );

                            desc48.putUnitDouble( idTop, idPxl, theArray[1] );

                            var idLeft = charIDToTypeID( "Left" );

                            desc48.putUnitDouble( idLeft, idPxl, theArray[0] );

                            var idBtom = charIDToTypeID( "Btom" );

                            desc48.putUnitDouble( idBtom, idPxl, theArray[3] );

                            var idRght = charIDToTypeID( "Rght" );

                            desc48.putUnitDouble( idRght, idPxl, theArray[2] );

                            var idtopRight = stringIDToTypeID( "topRight" );

                            desc48.putUnitDouble( idtopRight, idPxl, 0.000000 );

                            var idtopLeft = stringIDToTypeID( "topLeft" );

                            desc48.putUnitDouble( idtopLeft, idPxl, 0.000000 );

                            var idbottomLeft = stringIDToTypeID( "bottomLeft" );

                            desc48.putUnitDouble( idbottomLeft, idPxl, 0.000000 );

                            var idbottomRight = stringIDToTypeID( "bottomRight" );

                            desc48.putUnitDouble( idbottomRight, idPxl, 0.000000 );

                        var idRctn = charIDToTypeID( "Rctn" );

                        desc45.putObject( idShp, idRctn, desc48 );

                    desc44.putObject( idUsng, idcontentLayer, desc45 );

                    var idLyrI = charIDToTypeID( "LyrI" );

                    desc44.putInteger( idLyrI, 5 );

                executeAction( idMk, desc44, DialogModes.NO );


                • 6. Re: Convert an image to an 'informative'color gradient
                  c.pfaffenbichler Level 8

                  Here are screenshots of results the above Script creates for two images:


                  I make no guarantees about the math (or the Script itself) but in my tests the results seem plausible.

                  • 7. Re: Convert an image to an 'informative'color gradient
                    Arvid.Stark Level 1

                    Thank you so much!


                    As you said, it looks plausible! I will try to do it with the images I have.


                    Thanks once again.

                    • 8. Re: Convert an image to an 'informative'color gradient
                      c.pfaffenbichler Level 8

                      Just give it a couple minutes, it creates (up to 256) Shape Layers so that can take a while.