Compare pixel values in photoshop

Issue

i want to make a little photoshop javascript. Technically, i just need to know how to compare the color values of pixels af if they were an array with three integer values in each, for example: (pseudocode)

for all pixels x
    for all pixels y
        if left pixel's green channel is bigger than red channel:
            set the blue channel to 25
        else
            if the blue channel is greater than 50
                set the green channel to 0

in the documentation, there’s a ton of things like filters, text and layers you can do, but how do you do something as simple as this?

Solution

Reading and writing pixel values in Photoshop scripts is indeed not as simple as it could be … Check out the following script which inverts the blue channel of an image:

var doc = app.open(new File("~/Desktop/test1.bmp"));

var sampler = doc.colorSamplers.add([0, 0]);

for (var x = 0; x < doc.width; ++x) {
    for (var y = 0; y < doc.height; ++y) {        

        sampler.move([x, y]);
        var color = sampler.color;

        var region = [
            [x, y],
            [x + 1, y],
            [x + 1, y + 1],
            [x, y + 1],
            [x, y]
        ];

        var newColor = new SolidColor();
        newColor.rgb.red = color.rgb.red;
        newColor.rgb.green = 255 - color.rgb.green;
        newColor.rgb.blue = color.rgb.blue;

        doc.selection.select(region);
        doc.selection.fill(newColor);

    }
}

I’m not sure if there’s a prettier way of setting a pixel color than the select + fill trick.

This script runs super slow, so maybe Photoshop scripts are not the best tool for pixel manipulation …

Answered By – wutz

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published