Manipulate an Image with Scripting

Manipulate an Image with Scripting

Manipulate an Image with Scripting
docRef_1.layers[0].duplicate(); docRef_1.activeLayer = docRef_1.layers[0]; docRef_1.activeLayer.applyAverage();

The first line looks familiar, we used the same command in step 3 but here we have replaced backgroundLayer with layers[0]. To understand this we need to know a bit about arrays in JavaScript. Arrays are simply just variables which hold more than one value, where variables are like boxes with a single piece of information, arrays are like a big boxes with lots of smaller boxes labelled zero to infinity inside. For example if you wanted to create an array holding the first three days of the week you would type:

var days = ["Monday", "Tuesday", "Wednesday"]

Now if you wanted to use the first day of the week you would use the code:


This would equal "Monday"

Obviously don't include these last two lines in your script. Now that you know a bit about arrays we can go back to the three lines of code we added. In the first line layers is an array which includes all the layers in our document. The top layer in the stack is always the first in the array, corresponding to layers[0] because arrays start at 0 instead of 1.

The second line sets the active layer to the top layer this is the same as selecting a layer in the layer panel in Photoshop. We didn't need to change the active layer but it makes things easier later on.

The last line applies the average blur, the format here is the same as most of the commands we have used so far and is what's called a method and usually takes the form of document.layer.function where the layer is the active layer and the function is applying the average blur. This can be done in Photoshop by going filter>blur>average.

Again, test the script and you should have a new layer above the image filled with a solid color.



This step is slightly more complicated, what we want to do is find the RGB values of the color we have in this layer. There is no really simple way of doing this in a script but the method I use is to have the script select first the red channel then get the histogram of that channel then go through the histogram from 0 to 255 until it finds a value that isn't zero then that value will be the R value, it then repeats that for the green and blue channels. It is easier to explain this by going through this process in Photoshop. If you don't still have the document open from when you tested it in the last step then run your script again. Okay go to the channels by going window>channels and select the red channel this should now hide all the other channels and the image will look grayscale. Next hit Ctrl+L to go to the levels adjustments, you will see a histogram with one vertical line in it, now drag one of the output levels sliders to roughly below the line and the number in the corresponding box is the R value. Getting the G and B value is done in the same way. The script to do this for the R value is shown below, from this you should be able to work out the code for the G and B value.

Pages: 1 2 3 4 5 6 7 8 9

One comment on “Manipulate an Image with Scripting”

Leave a Reply

Your email address will not be published. Required fields are marked *