Two images into one bmp with alpha channel

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
AdamCook
Posts: 3
Joined: 2016-03-01T12:09:13-07:00
Authentication code: 1151

Two images into one bmp with alpha channel

Post by AdamCook »

Hello there. So the situation is that I have
file_name_12345.png
file_name_12345_mask.png
Image + Image
and I want these two merged into one bmp, with the _mask file becoming the alpha channel. Then this repeated about 500 more times for other similarly named file pairings in the same folder.

Is this possible with ImageMagick? If it is, would anyone be kind enough to walk me through this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Two images into one bmp with alpha channel

Post by snibgo »

This should do the trick:

Code: Select all

file_name_12345.png file_name_12345_mask.png -compose CopyOpacity -composite out.bmp
snibgo's IM pages: im.snibgo.com
AdamCook
Posts: 3
Joined: 2016-03-01T12:09:13-07:00
Authentication code: 1151

Re: Two images into one bmp with alpha channel

Post by AdamCook »

That works like a charm. Thank you!

But I can only do it for one pair at a time manually. Would there be a way to automate this?

Something like
for every ***.png select the respective ***_mask.png -compose CopyOpacity -composite D:\output folder\***.bmp
so it goes through every pair in the folder and also names the resulting bmp after the first input file?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Two images into one bmp with alpha channel

Post by GeeMack »

AdamCook wrote:But I can only do it for one pair at a time manually. Would there be a way to automate this?

Something like
for every ***.png select the respective ***_mask.png -compose CopyOpacity -composite D:\output folder\***.bmp
so it goes through every pair in the folder and also names the resulting bmp after the first input file?
There are several ways to approach this. Some would depend on whether you're on a Windows or a *nix OS, and maybe what version of IM you're using.

I'm using ImageMagick 6.9.3-6 64 bit on Windows 7 64. At the very simplest, if the number of image files and the number of mask files are the same, and if the new names can be just sequentially numbered and not necessarily relate to the input files, something like this should work...

Code: Select all

convert img*.png null: mask*.png -compose copyopacity -layers composite out%03d.bmp
That would bring all the "img*.png" files into the command.

Then it adds the IM built-in "null:" image as a separator between the images and the masks.

Then it brings in all the "mask*.png" images.

Then it sets the compose method as "copyopacity".

Then it runs the "-layers composite" operation, which is essentially the same thing as the "-composite" operator with an important difference. It can work on lists of files separated by that "null:" image. It takes the first "img*.png" and performs the composite operation with the first "mask*.png", then takes the second "img*.png" and performs the composite operation with the second "mask*.png", and so on until all the images in both lists are used up.

Then it names the output files "out%03d.bmp". That's "out" followed by a number padded to 3 places starting with 0, and ending with ".bmp", like "out000.bmp", "out001.bmp", "out002.bmp", etc.

The entire directory is processed with one fairly uncomplicated variation of the command snibgo provided, but... If the output files must meet some other naming criteria, or if the input files aren't named so they can be easily read into the command with wildcards, you might need to use a "for" loop of some sort. That would require varying syntax depending on the platform.

----------

EDITED TO ADD: Make sure you use appropriate wildcards for your OS so the list of images and the list of masks are brought into the convert command separately. For example, "file_name_*.png" would pick up "file_name_12345.png" and "file_name_12345_mask.png", and that's not what you want.

Also, I was looking at some other material and found a way to use the file names of your input images as part of the output file name using a command like this...

Code: Select all

convert ^
   file_name_?????.png ^
   -set filename:f "%t" ^
   null: ^
   file_name_?????_mask.png ^
   -compose copyopacity ^
   -layers composite ^
      "%[filename:f]_alpha.bmp"
That "-set filename:f" loads a special variable with the name of the image file without the extension using "%t". Then it uses that variable with a percent sign and square bracket, "%[filename:f]" plus the "_alpha.bmp" to build the output file name on the final line.

Notice in the command I used Windows convention of carets "^" to escape the newlines and double quotes around the file names. The same command in a Windows BAT script would require doubling the percent signs "%%". At a *nix command prompt I'm pretty sure you'd use backslashes "\" to escape newlines and single quotes around the file names.
AdamCook
Posts: 3
Joined: 2016-03-01T12:09:13-07:00
Authentication code: 1151

Re: Two images into one bmp with alpha channel

Post by AdamCook »

That's absolutely perfect. Thank you so much for the help and making it easy to understand.
Post Reply