Channel Packing Between Images

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
brycetclark
Posts: 3
Joined: 2015-05-04T18:12:13-07:00
Authentication code: 6789

Channel Packing Between Images

Post by brycetclark »

Hello folks,

I've been reading the documentation and I did some forum searching, but I'm still rather unclear as to how to accomplish my specific goals with ImageMagick - though it seems like it should be a trivial task. Edit: To be clear, I'm looking to do this via command line.

I'm looking to take a series of images:

test_albedo.tga, test_alpha.tga, test_roughness.tga, test_metallic.tga

And pack their channels together in a specific way, taking RGB or specific channels from image 1 and packing that data into a channel of image 2.

E.G.

output_albedo_roughness.tga: RGB from test_albedo RGB, Alpha from test_roughness G(or any color channel)

output_metallic_alpha: RGB from test_metallic, Alpha from test_alpha(any channel)

Thanks in advance for any help, I'm excited to make use of ImageMagick, once I get a hold of utilizing it!

Regards,

Bryce
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Channel Packing Between Images

Post by fmw42 »

Separate the channels of each image using -separate. Then use -combine to take individual channel images and put them back into RGB or RGBA format.

See
http://www.imagemagick.org/Usage/color_basics/#channels
brycetclark
Posts: 3
Joined: 2015-05-04T18:12:13-07:00
Authentication code: 6789

Re: Channel Packing Between Images

Post by brycetclark »

fmw42,

Thank you for the response. I had considered doing that, though it seemed like it might be an unnecessary step. Is this really the only way to accomplish my goals, to split the channels and then recombine them all, instead of only adding/changing specific channels?

Regards,

Bryce
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Channel Packing Between Images

Post by fmw42 »

If you know which channels you want from each image that you want to combine, you can separate only those channels and combine them in one command. You will need to use parenthesis processing to keep it all in one command.

If you have one image and only want to replace one channel or swap channel, then you can use -swap, -delete, -insert, ect to switch channels around, but you still need to read in and separate the channel that is to be used as a replacement.

see
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#list_ops
http://www.imagemagick.org/script/comma ... hp#channel

For example, if you have one sRGB image and want to replace the second channel of that image with channel 1 of another image, you could do that as
(unix syntax)

Code: Select all

convert image1 -separate +channel \
\( image2 -channel red -separate +channel \) \
-swap 1,3 +delete -combine resultimage
note image numbers start at 0 in the command line and channels are referenced as r, g, b, a
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Channel Packing Between Images

Post by snibgo »

brycetclark wrote:output_albedo_roughness.tga: RGB from test_albedo RGB, Alpha from test_roughness G(or any color channel)
I would do it like this (Windows BAT syntax):

Code: Select all

%IM%convert ^
  in1.png ^
  ( in2.png ^
    -channel G -separate +channel ^
  ) ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  output.png
The output has RGB from in1.png, and alpha from the G channel of in2.png.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Channel Packing Between Images

Post by fmw42 »

Yes, snibgo is correct. It is easier if all you want to do is add an alpha channel from another image. No swapping is needed.

see
http://www.imagemagick.org/Usage/compose/#copyopacity
brycetclark
Posts: 3
Joined: 2015-05-04T18:12:13-07:00
Authentication code: 6789

Re: Channel Packing Between Images

Post by brycetclark »

This information was very helpful in achieving my goals. Thanks fmw42 and snibgo!
Post Reply