Menu Close

Aligning images via Selection

Objects with a shadow cannot be trimmed as the shadow should be sticking out of the margins

Now let’s see some other examples of images that need to be aligned and that cannot be aligned via Trim. What kind of images are those? Well, in fact, any images except simple mannequins and shadowless objects. Like, for example, objects that have to be isolated with a natural shadow. A natural shadow means that it will be from the original image, not drawn or created in Photoshop by dropping a shadow under an object. And this shadow is the reason why you can't use the Trim command to align the image.

Some contents or functionalities here are not available due to your cookie preferences!

This happens because the functionality/content marked as “Vimeo framework” uses cookies that you choosed to keep disabled. In order to view this content or use this functionality, please enable cookies: click here to open your cookie preferences.

I need an image of an object that is standing on the background and dropping a natural shadow. In the process of isolation, which has already been discussed, and thoroughly, I will not do anything special with the shadow, except that making sure that I’ve selected all the background around it. After doing the isolation with the help of the isolating action, I will use the History brush after making sure that the brush tip is soft and carefully draw over the shadow, especially over the hard edges. That’s it.

Now see, if you want to align the image with the Trim-based action, it will not work properly because of the shadow. The margin will be under the shadow, and it should be under the object, across the shadow. That’s the problem. Now you might say: “Okay, it’s not a big deal, I’ll just transform the image and stretch it”, but this is absolutely not acceptable. There are two reasons why you can’t just go and stretch images: the first one is that you have to do it manually, which is time consuming, and the second one is that transforming an image after resizing will drop its quality quite dramatically. The stretched image will not be sharp, and what’s the point working with high resolution images, what’s the point with all the megapixels and the matrix size if you do something like that?


But what can we do? What we need is let Photoshop know somehow where the margins should be, that the bottom margin should be between the shadow and the object. How can we do that? Either let it try and determine it automatically (which we will do later) or just show it with something that it can understand. Like a selection. I usually use the Rectangular Marquee for that. The selection should barely touch the object around, and the shadow should be excluded from it.

Unlike the Trim-based action, Selection-based action script is much more complex, because it has to perform a lot of calculations to resize the image. We can’t just trim the image and add some canvas to it because if we do that, we’ll destroy the shadow. What we can do is transform the initial selection according to the guidelines, and then resize the image. To understand how it is done you need to have some knowledge of math, geometry, and programming altogether, and I believe to most people this is not something they find interesting or necessary. At least almost all the retouchers that I’ve worked with never wondered how exactly the scripts worked as long as they worked. So I’ve decided to make it short, and encourage those who are interested in details to find it out by themselves.

Now, when we have the selection active, the rest will be done by a script. First of all, we have to declare and assign all the necessary variables like image width, image height, resolution, top margin height, bottom margin height, left margin width, right margin width, and we also need to let the script know how the object should be oriented, should it be centered or should it stick to a lower margin, or maybe to the upper margin. I also usually add a couple of other variables to make some of the calculations easier, like the object width, and the object height. And don't forget about the width to height ratio, so that the script will be able to determine if the image is high or wide, because it will affect the alignment.

To operate selections via a script, we need to understand how Photoshop treats those selections. To define a selection we need four values. Every pair of values is the coordinates of the upper left and lower right corners of the selection. By default, they are named 0,1,2,3, but I usually name them A, B, C and D, because we have a lot of numbers to deal with already. A is the x coordinate of the upper left corner and B is the y coordinate, C is the x coordinate of the lower right corner and D is the y coordinate. We can add to or subtract from each value and it will affect the selection.

So what does the script do? It has to check if there’s an active selection somewhere in the active document, which is our image. Because if there’s no selection, nothing can be done, and the script will alert the user in this case. If there is a selection, its coordinates are used to transform it. The selection width is expanded according to the left and right margins, the height is expanded according to the top and bottom margins, also having in mind that we must either center the image or stick it to the bottom (or top) margin.

When we have all the necessary variables declared and assigned, and all the conditions specified, all that's left to do is just make a selection of the object in the image and run the script.

Some contents or functionalities here are not available due to your cookie preferences!

This happens because the functionality/content marked as “Vimeo framework” uses cookies that you choosed to keep disabled. In order to view this content or use this functionality, please enable cookies: click here to open your cookie preferences.

The script makes a series of calculations to transform the initial selection, then it resizes the image according to the new boundaries and also draws all the guides so that the user can see if it went alright or not. It can go wrong only if the initial selection was wrong, of course. This kind of alignment, which I call selection-based, is much more flexible than trim-based. Because there are certain cases when trimming an image doesn't work. Sometimes you'd want the object to stick out of the margins. Like this bow tie – the strap is not supposed to be fit within the margins. It's supposed to be cut off by the image borders because the length of the strap is not important, the focus should be on the bow tie itself. So if I aligned the image via Trim, I would not be able to cut the strap, as it's a part of an object. I could only get rid of it if I cut it off in reality, like selecting the bow tie, inverting the selection and filling it with white. Sounds like a hassle, while all these extra steps could be completely avoided if I used a selection instead.

It gets transformed according to the variables in the script and the object gets aligned with no straps, as they were not included in the selection. This is how we can easily control alignment depending on how exactly we want the final image to look.

If I want to align the bag with a shadow underneath, I can do it by using the selection-based aligning method. All I have to do is make a selection so that it shows the script where the border between the bag and its shadow is. Then I'll run the action and it will align the image perfectly.


Can something go wrong with the alignment? Not really. If the selection is correct, it will work fine, it's as easy as that. Won't work if there's no selection in the image, which is obvious.

Now we have a mechanism that allows us to align any image via a selection. Is it good? Sure. Unlike the Trim-based method, it allows us to make objects stick out of the margins, which is sometimes necessary even with the mannequins – like when there's a long belt hanging far below the dress and things like that. But there’s only one problem left. With this method, I have to make a selection manually to make it work. So what if I don’t want to? Can it be automated even further? Yes, it can. And I will explain it on the next page.

Next: Aligning images via Alpha channel


On this website, we use first or third-party tools that store small files (cookies) on your device. Cookies are normally used to allow the site to run properly (technical cookies), to generate navigation usage reports (statistics cookies) and to suitable advertise our services/products (profiling cookies). We can directly use technical cookies, but you have the right to choose whether or not to enable statistical and profiling cookies. Enabling these cookies, you help us to offer you a better experience. If you choose to disable cookies, you won’t be able to watch the embedded videos. Cookie policy