How to divide this command into separate sub-commands?

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
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

How to divide this command into separate sub-commands?

Post by Pit Sullivan »

Hello, guys!
There is a command:

Code: Select all

convert original.jpg \( -clone 0 -fill 'color' -colorize 100% \) \( -clone 0 -colorspace gray -negate \) -composite output.jpg 
So, I wonder how I can divide this command into separate pieces.
I know how to create these two additional images which where being cloned from the original image and then customized, but when I try to 'composite' it together, the result always differs from that one I get with this command.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to divide this command into separate sub-commands?

Post by fmw42 »

What version of Imagemagick are you using and what platform? There should be no difference if you do them separately with a current version of IM. How are you specifying 'color'? As a name, as an rgb triplet or as a hex value?

With IM 6.9.1.1 Q16 Mac OSX, I get virtually the exact same results from:

Code: Select all

convert logo: \( -clone 0 -fill red -colorize 100% \) \( -clone 0 -colorspace gray -negate \) -compose over -composite result1.png

Code: Select all

convert logo: -fill red -colorize 100% tmp1.png
convert logo: -colorspace gray -negate tmp2.png
convert logo: tmp1.png tmp2.png -compose over -composite result2.png

Code: Select all

compare -metric rmse result1.png result2.png null:
35.2278 (0.000537542)
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

Re: How to divide this command into separate sub-commands?

Post by Pit Sullivan »

fmw42 wrote:What version of Imagemagick are you using and what platform? There should be no difference if you do them separately with a current version of IM. How are you specifying 'color'? As a name, as an rgb triplet or as a hex value?

With IM 6.9.1.1 Q16 Mac OSX, I get virtually the exact same results from:

Code: Select all

convert logo: \( -clone 0 -fill red -colorize 100% \) \( -clone 0 -colorspace gray -negate \) -compose over -composite result1.png

Code: Select all

convert logo: -fill red -colorize 100% tmp1.png
convert logo: -colorspace gray -negate tmp2.png
convert logo: tmp1.png tmp2.png -compose over -composite result2.png

Code: Select all

compare -metric rmse result1.png result2.png null:
35.2278 (0.000537542)
It works, thank you so much! I was doing it wrong. I have used 'composite' command. I'm using IM 6.9.1.1 Q16 on Ubuntu 14.04. I've just wanted to learn how this command works. I thought It would help me to write a C++ program that does the same things. But, you know, there are only three composite methods in the Magick++ library's Image class. All of them have only one input parameter of type Image. It means I can 'composite' only two images at the same time, doesn't it? So what should I do? Should I invoke composite method on original image object with a colorized image object as an argument and then repeat the same thing for the result of this operation and negated image?
I've just started to learn the library. Thank you for your help, I appreciate it!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to divide this command into separate sub-commands?

Post by fmw42 »

I will contact the IM developers and see if this is truly a limitation of Magick++. Perhaps they can make a change to the Magick++ composite.
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

Re: How to divide this command into separate sub-commands?

Post by Pit Sullivan »

fmw42 wrote:I will contact the IM developers and see if this is truly a limitation of Magick++. Perhaps they can make a change to the Magick++ composite.
But how does the convert program do it? It probably uses the same resources that Magick++ does. Today I have tried to figure it out but it's too complex for me. Thank you anyway!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to divide this command into separate sub-commands?

Post by fmw42 »

Magick++ does not use the same code per se. It may use the same base code, but not all features of the command line are fully implemented in Magick++. Usually when some one needs some feature, the developers will add it as time and complexity permit.
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

Re: How to divide this command into separate sub-commands?

Post by Pit Sullivan »

UPD: I have finally figured it out. There is no limitation. Let's examine this command:

Code: Select all

convert background.jpg source.jpg mask.jpg -composite result.jpg
We have three images here - background, source and masking image which limit the area affected by the 'compose' method. Magick++ library provides a method called 'mask', which let us add a mask to our Image object. So I tried to add the mask to background Image object and then composed background and source images together using 'composite' method like this:

Code: Select all

// code C++
Magick::Image background("background.jpg");
Magick::Image source("source.jpg");
Magick::Image mask("mask.jpg");

background.mask(mask);
background.composite(source, 0, 0);
background.write("result.jpg");
And it works! Both images from command line and from my program are absolutely the same. But there is another problem. When I try to change a compose method to 'blend', both images differ from each other, and I don't know why it is so. But I'm going to find out. Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to divide this command into separate sub-commands?

Post by fmw42 »

The order of the two first images may differ in the command line and in Magick++. Try swapping them. The command line order depends upon whether you use composite ... or convert ... -composite. They are opposite. But if you have a 50/50 blend, then this will not be an issue.
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

Re: How to divide this command into separate sub-commands?

Post by Pit Sullivan »

I've already tried, but if I swap them, I will get wrong result in previous case (calling composite with default composition operator).
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

Re: How to divide this command into separate sub-commands?

Post by Pit Sullivan »

UPD: Finally I have found a trial-and-error solution. I'm gonna put it here but first I need to figure out why when the negate option is disabled in my program but enabled in command-line and vice versa, I get the same result. Do you have any ideas?
Post Reply