5 Replies Latest reply on Mar 9, 2011 7:34 AM by Sintrea

    Selecting pixels in a picture and changing their color with a script

    Sintrea

      Hi, I'm brand new to photoshop scripting, and I want to make a script that takes every pixel of a color and replaces it with a new color,

      I'm having the hardest time finding any information on the internet on how to do it though.

       

      I do have some background in other coding, so the concepts are not new, just the language and functions.

       

      I know there's tools and filters already, but I want to make a script so I can add features.

       

      Any help would be appreciated.

       

      Thank you!

       

      (P.S. Is there a way to open and read code of a pre-exsisting filter, or is that impossible?)

        • 1. Re: Selecting pixels in a picture and changing their color with a script
          c.pfaffenbichler Level 9

          When you write »pixel of a color« do you really mean one specific RGB-value?

          Because then it might be easiest to just create one pixel of the color, load that as a Selection and use

          Selection.similar(0, false)

          and use the Selection thus produced to create a Solid Color Layer.

           

          Having the Script iterate through each pixel’s values individually might take considerably longer.

           

          (P.S. Is there a way to open and read code of a pre-exsisting filter, or is that impossible?)

          I wonder if what Scripting offers is what you are after if you want to muck about with Filters’ code.

          Have you checked out Pixel Bender yet?

          • 2. Re: Selecting pixels in a picture and changing their color with a script
            Sintrea Level 1

            Yes, one specific color value, RBG

             

            Why would pixel bender be useful? I tried it and it's just a bunch of filters, I want to edit filters, am I missing something?

             

            Thanks

            • 3. Re: Selecting pixels in a picture and changing their color with a script
              Paul Riggott Level 6

              To create your own filters you would need to download the SDK I think you might find an example of a filter.

               

              With Photoshop this might be close...

               

              main();
              function main(){
              if(!documents.length) return;
              var sColour =new SolidColor;
              sColour.rgb.hexValue ='245887'; 
              selectColorRange(0,sColour);
              try{
               var SB = activeDocument.selection.bounds;
                  }catch(e){return;}
              var FillColour =new SolidColor;
              FillColour.rgb.hexValue = 'ff0000';
              activeDocument.selection.fill(FillColour,ColorBlendMode.NORMAL);
              activeDocument.selection.deselect();
              }
              function selectColorRange( fuzz, solidColor){
              var scaleA = (solidColor.lab.a + 128.5) / 256;
                  var desc11 = new ActionDescriptor();
                  desc11.putInteger( charIDToTypeID( "Fzns" ), fuzz );
                      var desc12 = new ActionDescriptor();
                      desc12.putDouble( charIDToTypeID( "Lmnc" ), solidColor.lab.l );
                      desc12.putDouble( charIDToTypeID( "A   " ), solidColor.lab.a + scaleA);
                      desc12.putDouble( charIDToTypeID( "B   " ), solidColor.lab.b + scaleA);
                  desc11.putObject( charIDToTypeID( "Mnm " ), charIDToTypeID( "LbCl"), desc12 );
                  desc11.putObject( charIDToTypeID( "Mxm " ), charIDToTypeID( "LbCl" ), desc12 );
              executeAction( charIDToTypeID( "ClrR" ), desc11, DialogModes.NO );
              };
              


              Using Christophs suggestion...

               

              //Use 1 pixel to colour
              app.activeDocument.colorSamplers.removeAll();
              var mySampler = app.activeDocument.colorSamplers.add([new UnitValue(1,'px'),new UnitValue(1,'px' )]);
              //Get original colour
              var oColour = new SolidColor();
              oColour = mySampler.color;
              app.activeDocument.colorSamplers.removeAll();
              //Select colour
              var sColour = new SolidColor();
              sColour.rgb.hexValue ='31618f';
              activeDocument.selection.select([[0,0],[1,0],[1,1],[0,1]], SelectionType.REPLACE, 0, false);
              activeDocument.selection.fill(sColour,ColorBlendMode.NORMAL);
              //Replacement Colour
              var rColour = new SolidColor();
              rColour.rgb.hexValue ='ff0000';
              activeDocument.selection.similar(0, false);
              activeDocument.selection.fill(rColour,ColorBlendMode.NORMAL);
              activeDocument.selection.deselect();
              //Replace original colour.
              activeDocument.selection.select([[0,0],[1,0],[1,1],[0,1]], SelectionType.REPLACE, 0, false);
              activeDocument.selection.fill(oColour,ColorBlendMode.NORMAL);
              activeDocument.selection.deselect();
              
              1 person found this helpful
              • 4. Re: Selecting pixels in a picture and changing their color with a script
                c.pfaffenbichler Level 9

                Paul, I think the concept in your first Script makes much more sense than my idea …

                 

                Why would pixel bender be useful? I tried it and it's just a bunch of filters, I want to edit filters, am I missing something?

                Sintrea, have you read the first paragraph here?

                http://www.adobe.com/devnet/pixelbender.html

                One can create and edit Pixel Bender Filters oneself, but again Paul’s suggestion is a good one if you really want to go into Filter creation.

                • 5. Re: Selecting pixels in a picture and changing their color with a script
                  Sintrea Level 1

                  Yes Paul's answer is very helpful, thank you.

                   

                  About Pixel Bender, I think I downloaded the wrong thing, oops!

                  I'll have to try it later, thanks.