Manipulate an Image with Scripting

Manipulate an Image with Scripting

Manipulate an Image with Scripting

After this 'if' statement we have an 'else' statement which means if the width is larger than the height then it will execute the command contained within these curly brackets; which is similar to the other command.

The last line resizes the canvas to a width and height equal to borderSize added to the width and height. Note that the 1px border around these images is just added in because the background is also white.


Step 13:

Now if you tested the script you might think it looks fine however the background layer will resize to fit any size of canvas so we need to have the script create a new layer with the border in it then merge this layer and the image layer. The code for this is:

docRef_1.artLayers.add(); docRef_1.activeLayer.move(docRef_1.backgroundLayer, ElementPlacement.PLACEBEFORE); docRef_1.selection.selectAll(); docRef_1.selection.fill(white); docRef_1.selection.deselect(); docRef_1.layers[0].merge();

The only part of this code that we haven't seen before is the move command which we are using on the active layer which will be the one we just created. The first part inside the brackets is the reference layer which we have defined as the background layer, the second part tells Photoshop to move the active layer to before the reference layer using ElementPlacement.PLACEBEFORE.

Step 14:

Now we are going to add what will become the shadow, at the moment this will just be a layer directly before the background layer filled with black. Here is the code:

docRef_1.artLayers.add(); docRef_1.activeLayer.move(docRef_1.backgroundLayer, ElementPlacement.PLACEBEFORE); docRef_1.selection.selectAll(); docRef_1.selection.fill(black); docRef_1.selection.deselect();

This code is nearly exactly the same as the code in the last step except that this is filled black and we don’t merge it yet. If you test it shouldn't look any different from what it did in the last step.

Step 15:

Okay now we want the script to resize the canvas so it can fit the image at any angle; this requires good old bit of Pythagoras. The code for this is:

var width = docRef_1.width; var height = docRef_1.height; var newSize = Math.round(Math.sqrt(width * width + height * height)) + borderSize; docRef_1.resizeCanvas(newSize, newSize);

The first two lines create new variables for the width and height. The next line create a variable for the new size of the document, the Math.sqrt function in this takes the square root of the number in the brackets and we add the borderSize just to account for the shadow we will create. The last line is resizing the canvas.


Step 16:

Now to create the shadow, we want the script to add a gaussian blur to the black layer we created earlier. I found that the value for the gaussian blur should be a third of the borderSize and the opacity, 50%. Here's the code for this part:

Pages: 1 2 3 4 5 6 7 8 9

Luminar: AI Photo Editing

Luminar AI lets you turn your ideas into reality with powerful, intelligent AI. Download the photo editor and see how you can completely transform your photos in a few clicks.

One comment on “Manipulate an Image with Scripting”

Leave a Reply

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