Menu Close

Aligning images via Trim

A perfect example to demonstrate how trimming-based alignment works

Now, I have this mannequin image here, and I want to align it to fit some specific values like image size, margins width, resolution, etc. How can I do that? The task at hand is relatively easy because the image has to touch the margins, and no parts of the image should be sticking outside. If this mannequin had a shadow underneath, it would be very reasonable if the shadow was not within the margins but outside. But let’s leave more difficult cases for later, after you learn how to deal with the simple ones. Forget about the shadows, for now, we’re going to build an automatic process to resize shadowless objects and make them fit the margins.

I also want to add that there's another way how to align isolated images without having to resort to script writing. There's the Fit Image command in the File menu, Automate submenu. After you've trimmed your images, you can use the Fit Image command to make Photoshop fit your image to the final image size without margins. It will be able to fit your image by doing basically the same thing as I do in the script – evaluating the width and height ratio and changing the image size according to that. You can record it into an action after the Trim command, and then just add the canvas you need. The action will work on both high and wide images. But this method is only good for the trimmed shadowless centered images, which is not satisfying enough. Because of that, I'm not going to show you how this Fit Image thing works at all.

The reason why I was able to achieve so much in image aligning is that the Fit Image command was not available in Photoshop back then, in 2011. So the lack of simple means forced me to do all the scripting by myself and I've managed to learn a lot because of that. This is also the reason why I teach you the basics of scripting for Photoshop. If you don't have any idea how scripts function, you won't be able to use them efficiently.

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

This happens because the functionality/content marked as “Google Youtube” 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’ll record the further steps so that I won’t have to repeat them manually, as the resizing and margin fitting is very tedious. I’ll call it “Mannequin_fit”. The first step will be flattening the image in case there are some layers, which happens sometimes. Flattening will ensure that we only work with the background layer and it’s the only available layer. Otherwise, the action’s work might not be very stable with the layered images. The second step will be trimming the image in question so that it will have no margins at all. There’s a Trim command in the Image menu. It takes the color of a specified pixel, top left by default, and trims away everything of that particular color. In this case, it will trim all the white background surrounding the object. Now I’m going to resize the image so it fits our guidelines.

I’ll access the Image Size dialogue window by pressing Ctrl-Alt-I. The image should be 800 pixels wide, but having in mind that the object in question has no margins (they will be added later), so it should be 800 pixels minus both margins width, which is 50 and 50 pixels, so together it’s 100 pixels. 800 pixels minus 100 pixels is 700 pixels. Note that when I put 700 pixels in width, the height is going to change too, but it’s okay. And don’t forget about resolution, which has to be 72 pixels per inch, and whoops, when I change that the image size gets changed too. So it’s reasonable to change the resolution first, the size last. I’ll change that to 700 again and hit OK.

Good, but what about the image height and the margins? Let’s add the left and the right margins with the use of the Canvas size command. Canvas extension color should be white, of course. Now, our width is going to be 800 pixels. When I hit okay, the image becomes wider, and this added width equals to the margins we need. But what about the height? Let’s check out guidelines, that say “1200 pixels height, margins: 100 pixels top & bottom, mannequin orientation – upper margin”. It means that the image should be 1200 pixels high and that all the mannequins have to stick to the upper margin. To achieve that, I’m going to add an upper margin by accessing the Canvas Size again and ticking the “Relative” checkbox, which allows me to enter the desired value. So I’ll put 100 in the Height input window and click on the bottom arrow to show the algorithm that I wish it to work on the opposite side. Great, now we have the upper margin as well!

The final step will be fixing our image size without ruining the alignment. I’ll do it with the Canvas Size command by unticking the “Relative” checkbox and entering 1200 into the height input window. I’ll also click on the top arrow to ensure that the canvas will be extended from the bottom. Now we have our image aligned. If I run the guides making action that I recorded previously, you will see that the mannequin fits perfectly. But is that it? Well, as a matter of fact, there is one huge problem with the action I’ve just made. I’ll test it on a few images and it will become evident.

Let’s try it on another image. Works fine.

And what about this fur coat? Uh-oh. Seems like it’s cut, but why did that happen? The problem is that when we resize the T-shirt to fit the margins, we adjust its width, we make it touch the right and left margins because it’s not long enough to touch both the top and bottom margins. And the fur coat should be aligned in a different way, it has to touch the top and bottom margins instead. We seem to be in need of a new action.

I’ll copy the previous action to save myself some time first. And I’m going to rename the first action so it’s called Mannequin_fit_width, and the other one will be Mannequin_fit_height. I will now rewrite some steps to make it do what it has to. Flatten and Trim steps are okay, but everything else has to be rewritten. You can easily rewrite any command in an action by clicking it twice. Let’s start with the Image Size and concentrate on the height. It should be 1000 pixels, which is 1200 total height minus 200 pixels of margins. But to do this we don’t need three Canvas Size commands. Actually, we could have used just two canvas size commands in the first action, but I did everything separately for a better understanding on your side. And now we can use just one Canvas Size command, as the image is centered now, we don’t have to stick it to the upper margin like before. So I’ll remove the three Canvas Size commands from the action by dragging them to the trash can and record a new one.

Here, I’ll put 1200 in height so that it adds two margins, the top and the bottom, 100 pixels each. And I’ll put 800 in width, and it will add some space until the image size is right. In this case I don’t use any arrows to show the direction, as we’re going to expand the canvas in all directions. Hit OK, and then play the guides drawing action to make sure everything is alright.

Open

Now we have two actions for the two types of mannequins. One of them deals with wide images and the other one deals with high images, so you have to decide which action is necessary in a particular case, but when you play any of these actions, everything is done without any need of manual adjustment, which is good.

But not good enough. I am still not satisfied with the results. Why? Because I want the alignment process to be fully automatic, and for now I still have to decide which action should be played. Why not just let the computer decide what to do, we’re in the 21st century after all. So we have a bunch of images, all the proportions are different and not known to us, and we need to align them. What we need here is to determine which of images should be considered wide, and which should be considered high, as we have a separate action for each of the two possible situations.

The image within the margins is basically a box with a specific size, which is 700 pixels wide and 1000 pixels high. I’ll use some of my basic math knowledge from school and say that we can divide the width of the image by its height and get a value, in this case, 700 divided by 1000 will give us 0.7. This is not some random value, it’s our width to height ratio. And I assume that if we have an image with the ratio that is more than this value, this image should be considered wide, and vice versa. If my assumption is right (and it is right because it's math), we can tell the computer to do the following: “If you divide the trimmed image’s width by its height and get 0.7 or more, run the Mannequin_fit_width action, and if you get less than that, run the Mannequin_fit_height action”.

When you hear a phrase that goes like “If something do something, else do something else” you can be pretty sure that you are facing a conditional. And fortunately for us, Photoshop can use conditionals and run actions depending on some specific conditions. The Actions menu even has the Insert Conditional command, but if you go check and see if there’s anything worth using there, you'll be disappointed. Photoshop's conditionals are pretty lame. There's nothing there that we need, there’s no such thing as width to height ratio, and simple Landscape and Square conditions are not satisfying enough.

As usual, if you want something done right, do it yourself. It’s time to write a script! It might sound like a task for a programmer, but I assure you, this case is very simple and requires no advanced knowledge or Javascript or serious programming skills. It really is that simple. I'll open the ExtendScript Toolkit and start writing in a new document, but it can be done in any text editor.

We need to declare and assign a few variables to address them later. The first one is going to be the doc variable, which is a short word for the app.activeDocument, which is the default Photoshop variable for the currently open document. The name is a bit long as it is. Careful, everything here is case sensitive, if you miss the capital D in Document, it’s not going to work.

var doc = app.activeDocument;

Before we do anything, it’s necessary to flatten the document just in case, and we also have to trim the image, because we’re interested in the object width and height, not the canvas size. There are special commands for that.

doc.flatten();
doc.trim(TrimType.TOPLEFT);

Parenthesis are important, too. Note that I am still using the doc variable.

The next variable will be called w and it will represent the image width – the new image width, as we have it trimmed now. I will now use the “doc” variable instead of the app.activeDocument to keep things short. Then, the next variable h is our image height. And our last variable, which I’ll call ratio can be calculated by dividing the width by height, or dividing w by h.

var w = doc.width;
var h = doc.height;
var ratio = w/h;

Now, let’s handle the conditional. It goes like this:

if (ratio > 0.7)
  doAction('Mannequin_fit_width', 'GUIDES');
else
doAction('Mannequin_fit_height', 'GUIDES');

It basically tells Photoshop to calculate the ratio and then compare it with the value of 0.7, and if it’s more than that, run the Mannequin_fit_width action from the “guides” set, which should, of course, exist among your actions. So, if the ratio is less than 0.7 or equal to it, the Mannequin_fit_height action will be run instead.

Pay attention to the fact that there's a dot in 0.7, not a comma (as in 0,7). If you put a comma there, it won't work. Normal programmers would also encapsulate blocks of code after "if" and "else" into curly braces, but I skipped them intentionally to simplify the whole thing even more.

Now just put copy all the lines into one script, or just use the following script, and run it in Photoshop via the ESTK or using any other script running methods I've described in the respective chapter of the course (LINK).

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

This happens because the functionality/content marked as “Google Youtube” 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.

Let’s check if it works. I’ll run the script directly from the toolkit. It will have the desired effect on the active document, which is now a t-shirt image. And now the same with the fur coat image. As you can see, it worked correctly in both cases. The last thing is to save the script and make it usable via the actions panel. I’m going to save it in a folder where I keep all my scripts, which, in my case is in the Scripts folder on the C hard drive.

Now I can delete the trim and the flatten commands from my mannequin fitting actions because the script will be doing it from now on. And I’ll start recording a new action, which I will call “Mannequin_fit” and while the recording is active I’ll go to the File menu, the Scripts submenu and click on the Browse command, and then I’ll choose the script that I’ve just saved. That’s it. Now the next time I’ll need to align an image, I can just hit a single button and Photoshop will take care of the rest. Well done!

Open

Troubleshooting

Now, what if it doesn't work? What if some of the objects don't get aligned properly, and end up hanging somewhere in the corner of the final image? Well, that might happen. The weakest point of this whole trim-based alignment method is the Trim command. It doesn't do anything with your isolation, it just trims the object. So, in case the object was not isolated properly, it will not be trimmed properly, and your alignment will fail. All you have to do in this case is to make sure the isolation is flawless. If there's some trash on the background, erase it and try again. Even a single non-white dot will get in the way of the alignment.

If there's some trash on the background, go back in history, erase it and align again

Am I satisfied now? Not really. It's not just because I need three actions, while I can only have one fully scripted action. It's not about scripting – of course, instead of running different actions from within the script, I could have just written what the script should do depending on the image proportions. It's easy. I don't like this whole trim-based system because what we have here works with the isolated images only, and it will not work correctly if there are shadows underneath the objects, and it also will not work if the objects are not isolated at all.

The other problem is that when I want to change the margins or how the image is going to be aligned, I can’t do it fast. For example, I want to make the objects stick to the bottom margin instead of the top margin, I have to do a lot of rewriting. Same if I want to change the image size or the margins width. So, we still have a lot of work to do to deal with all those problems. In the end, I want to have a working mechanism for all the catalogue images I might come across, not just the isolated mannequins.

You can download the images for practice purposes from the gallery above. Each thumbnail is linked to the respective hi-res image, just click on the thumbnails you need to open them in a separate window. You can right-click and "Save link as…" to download images without having to open them first.

Aligning images via Selection

Top

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