Skip to main content
March 8, 2011
Question

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

  • March 8, 2011
  • 1 reply
  • 3015 views

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?)

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
March 8, 2011

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?

March 8, 2011

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

Paul Riggott
Inspiring
March 8, 2011

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();