Adding/Subtracting Two 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
coolbeans201
Posts: 4
Joined: 2015-09-25T05:19:29-07:00
Authentication code: 1151

Adding/Subtracting Two Images

Post by coolbeans201 »

Hi all,

I'm new to ImageMagick as I have just started working on an image analysis project. I've working with some existing, but old, code for working with these images. I'm trying to understand how to add and subtract images as a test run with the code. Before, it looked like this:

Code: Select all

                        if (chosen_arithmetic == ADD)
			{
                            int status = execl("/usr/local/bin/combine", "combine", "-compose", "add", "temp_in1.pgm", "temp_in2.pgm", "temp_out.pgm", (char*)NULL);
			    if(status == -1) //error executing
			    {
					printf("Error executing.\n");
					exit(1);
			    }
			}
                        else if (chosen_arithmetic == SUBTRACT)
			{
                             int status = execl("/usr/local/bin/combine", "combine", "-compose", "subtract", "temp_in1.pgm", "temp_in2.pgm", "temp_out.pgm", (char*)NULL);
			     if(status == -1) //error executing
			     {
					printf("Error executing.\n");
					exit(1);
			     }
			}
I am assuming this no longer works due to newer versions being released since it was last worked on, but I want to know what I need to do now to have the same effect. Any ideas? Thanks so much!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding/Subtracting Two Images

Post by snibgo »

Is that ImageMagick? If so, it must be very old. What does "combine -version" say?

The current commands would be:

Code: Select all

convert temp_in1.pgm temp_in2.pgm -compose Plus -composite temp_out.pgm

convert temp_in1.pgm temp_in2.pgm -compose MinusSrc -composite temp_out.pgm
But the "MinusSrc" may need to be "MinusDst".
snibgo's IM pages: im.snibgo.com
coolbeans201
Posts: 4
Joined: 2015-09-25T05:19:29-07:00
Authentication code: 1151

Re: Adding/Subtracting Two Images

Post by coolbeans201 »

Yeah, this was last worked on 10 years ago. And I figured it out too. Just a silly logical error. Thanks!
coolbeans201
Posts: 4
Joined: 2015-09-25T05:19:29-07:00
Authentication code: 1151

Re: Adding/Subtracting Two Images

Post by coolbeans201 »

Also, what's the difference between what you have and this?

Code: Select all

int status = execl("/usr/bin/composite", "composite", "temp_in1.pgm", "temp_in2.pgm", "-compose", "plus", "temp_out.pgm", (char*)NULL);

Code: Select all

int status = execl("/usr/bin/composite", "composite", "temp_in1.pgm", "temp_in2.pgm", "-compose", "subtract", "temp_out.pgm", (char*)NULL);
I've seen how you can use either convert or composite for image manipulation, but is there any significant difference? Thanks again!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding/Subtracting Two Images

Post by snibgo »

coolbeans201 wrote:Also, what's the difference between what you have and this?
Your code is part of a C-program (or something). Mine is a command typed at the console. Was that the question?

In the old days, "composite" was used for one job, and "convert" was used for a different job. Since then, "convert" has gained almost all of the functionality of "composite". Very nearly everything that "composite", "convert" can also do, but it can also do much more.

I wouldn't use "composite" unless I had to. With "convert", we write the command in the logical, chronological order: read images, process images, write images.

One important difference: when these take two inputs, they are specified in the opposite order. So the following do the same:

Code: Select all

convert a.png b.png -compose Plus -composite out.png

composite b.png a.png -compose Plus out.png
snibgo's IM pages: im.snibgo.com
coolbeans201
Posts: 4
Joined: 2015-09-25T05:19:29-07:00
Authentication code: 1151

Re: Adding/Subtracting Two Images

Post by coolbeans201 »

My question was more along the lines of the difference between add, Plus, subtract, and MinusSrc. Are they the same for the operations they perform? I want to keep my code as similar to the functionality that existed before, so want to choose the right one.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding/Subtracting Two Images

Post by snibgo »

Sorry, I don't know what the "combine" program does or did. If it still works, you'll need to test it.

For the current "-compose" options, see http://www.imagemagick.org/Usage/compose/
snibgo's IM pages: im.snibgo.com
Post Reply