Menu Close

Aligning images via Alpha channel

Back in 2012, I was struggling with image alignment. I’ve had about 20 retouchers under my command and I was trying to increase their retouching capacity, that is, how many images each retoucher can retouch in a given amount of time, like a working day. I already went a long way from manual alignment to script-based alignment thanks to one retoucher who was familiar with Javascript programming and helped me a lot with all the coding. Everything worked rather smoothly except for one thing, and it kept bothering me. The faulty part of the process was the selection making. First of all, it had to be done manually. And if that wasn’t bad enough, if a retoucher was careless while drawing a selection, it affected the alignment. Could it be helped? Could the alignment be more precise?

And then the idea suddenly struck my mind like a bolt of lightning. Looking back at this situation, I realize that this particular idea was probably the smartest thing that ever came to my head. And it let me not just increase the retouchers’ capacity drastically, up to 500 images in a day, but also made the image alignment a very smooth, automatic and precise process. It saved a lot of time for the retouchers, and even now as I am telling you this, the images are being processed with the use of this particular idea, and by now more than two millions of items have been put online and sold.

You must be curious what the idea was, so here it is. I thought: “Okay, so I need to create a selection to align an image. But why do I need to draw it manually in the end? I already have a proper selection when I isolate the object, why not just reuse it for the alignment process?”.

Before we start talking about how this idea was implemented, we need to clarify how Photoshop can save selections, store and restore them. And by a selection I mean not just the marching ants outline. In fact, any selection contains quite a lot of information: for any given pixel of an image, it should be known if it’s selected and how opaque or transparent the selection is. Photoshop can save and store all this information in the form of a channel. This particular channel is called an alpha channel. Let’s see how it works.

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 save the selection that I’ve made into an alpha channel. To do it, I’ll go to the Select Menu and click on the Save Selection command. The only thing I have to do now is specify a name for the channel so that we can address it by the name later. I’ll call it “mch” which means “my channel”, but it doesn’t actually matter, it can be any word or a number. Now when I go to the Channels tab, the “mch” channel will be there and it will stay there until I choose to remove it. Remember, that if an image has an alpha channel, you will not be able to save it in jpeg format. But we’ll deal with it later, for now, let’s just see how it works.

Open

The best way to automate alpha channel handling is to let the isolation action create it, which is logical, and let the saving script or the alignment script remove it. You can only remove channels with a script though, don’t try to put it into an action. Because if the image has no alpha channels and you run the action that tries to remove it, it will give you an error. And if you put it in a script, that is done with a single line

app.activeDocument.channels.removeAll();

It will remove all the alpha channels no matter what their names are and how many channels there are, and if there are none, it will be okay, too, no error.

It's possible to use an action with a conditional, though. Photoshop can run different actions depending on some given conditions, so you can use this feature to avoid scripting, but that would be a rather clumsy solution. But let's get back to the alpha channel based alignment.

When I isolate images using the isolating action, the same selection of the background, that gets filled with white, goes into an alpha channel and stays there. Let’s see how it can be handled by a script, but before that I want you to pay attention to the following fact: to be valid, the selection must come across the border between the object and its shadow. And I have to keep this in mind in the process of isolation, but it’s relatively easy. Now every time I am to isolate an object, I’ll just make a selection with the Marquee Tool in such a way that it will separate the object from its shadow, and then I’ll use the Magic Wand and click on the leftover background. When it’s done and I run the isolating action, the selection will be saved in an alpha channel.

The next part is more complicated. We have to modify the script so it can deal with alpha channels. The first thing it has to do is check if there’s an active selection in the document. I want the selection to have a priority over the alpha channel. Why? Because the alpha channel matches the outline of the object, and sometimes I need to be a bit more specific to align an image than just that. Remember the bow tie? Sometimes alignment is not supposed to stick the whole object into the margins, so to be able to control that, we need selections to have a higher priority compared to alpha channels.

The script has to check if there’s an active selection in the image. If there is, it will start the alignment sequence based on it. If there is no selection, it will look for an alpha channel instead and try to load it. The only way to load an alpha channel with a script that I’ve found is to call it by the name. In this case, the name that is used in the isolating action should match the name used in the script. But there’s also a problem here: if there’s no alpha channel with such a name and the script tries to call it, it will cause an error. That’s why I have to use the “catch error” feature of the Javascript. It works very simply by trying to run some code. I specify what it has to try in the “try” section, and if it gives an error, it will run another code instead, that is in the “catch” section. And if trying the code from the “try” section doesn't give an error, then it will skip the “catch” section and proceed to the next steps of the script.

In any given situation, there are three possible outcomes. Outcome #1 is when there is an active selection, #2 is when there is no active selection but an alpha channel is present, and #3 is when there is neither a selection nor an alpha channel. If outcome #3 occurs, it means something went wrong and alignment cannot be done, and in this case, the script needs to alert the user. But if there is an alpha channel, the script will load it and turn into a selection. And when it has a selection, no matter if it was there or was loaded from the alpha channel, the script will run the alignment process and we’ll have our image aligned.

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.

With the alpha channel based alignment method, the whole process is very simple and easy. First, we isolate the image, and when we select the background we make sure that the selection comes across the border between the object and its shadow. Then we use the History Brush to bring the shadow back. Then we just run the script and get the image aligned.

Open

What can go wrong? In fact, many things. You can't imagine how many times I've heard from retouchers that my scripts and actions didn't work despite the fact that millions of images were successfully processed with their help. Most of the time the problem is on the user's side, so it's a good idea to tell you when exactly the alpha-channel based aligning method will not work and why.

Troubleshooting

First of all, as I mentioned before, the selection might not be perfect. If it doesn’t include all the background, and some parts of it such as dirt are left behind, it will affect the alignment process. So you have to be careful about that. If something goes wrong, you’ll have to clean the background. But even if you clean the background, all the dirt will still be present in the alpha channel. We need the script to deal with that, and it does exactly that: after loading the background selection, it grows it with Tolerance 1 to include all the areas that are now white on the Background but not selected in the alpha channel. This is easy and can be achieved by adding a line of code after the selection is loaded from the channel:

app.activeDocument.selection.grow(1, true);

Now, what’s growing a selection? Growing is expanding the selection with the tolerance specified. So if you have some trash on the background after running the isolating action, and it causes misalignment, all you have to do is undo the alignment, erase the trash and run the alignment action again. It will correct the alpha channel automatically, and no misalignment will occur this time.

There's a bit of trash left after isolation – that's why the alignment fails. Undo it, remove the trash and you're good to go

If a script fails to align the object, it doesn't mean that the script is faulty. It's just that the alpha channel is incorrect. And one of the reasons why it can be incorrect is that the background selection failed to include some trash or dirt. Undo the alignment, remove the dirt and you're good to go.

But that's not the only reason. Another one is this: an alpha channel is supposed to represent an outline of an object. What if I retouch it in such a way that the outline is changed? What if I Liquify or Transform it and the new object outline no longer resembles the old one, which is stored in the alpha channel? Well, too bad. If that happens, the script will attempt to align the image and it will fail. So how can we prevent that from happening? If the object didn't move but just became a bit smaller, this is not a problem at all. The same code sequence that grows the selection will resolve this issue. But if the object became bigger or moved, it won't work at all.

What you have to do is control your own workflow. Liquify and transform first, isolate after that. Don’t do anything that can alter the image outline after you’ve isolated the image.

With the use of scripts and actions, you can easily isolate and align all kinds of catalogue images. The process is strict, and you have to follow it precisely. If you did something wrong, go back and do it right. Watch your selection before you isolate, make sure it doesn’t eat away parts of the object and make sure that there's no trash left on the background.

Next: Aligning images via Edge detection

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