MagickWand C API equivalent to below command.

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

MagickWand C API equivalent to below command.

Post by rpatelob » 2017-04-24T06:48:22-07:00

Code: Select all

convert cnbc_r.jpg \
	 '(' cnbc_i.jpg +transparent '#0A0B0D' -alpha extract ')'\
	 '(' cnbc_p.jpg -rotate -33.75 +repage -gravity center -crop 600x437+0+0 +repage ')'\
	 '(' -clone 0 -clone 2 -compose softlight -composite ')'\
	 -delete 0,2 +swap -compose over -alpha off -compose copy_opacity -composite\
	 '(' +clone -alpha extract -write mpr:alpha -blur 0x4 -shade 130x30 mpr:alpha -alpha off\
	 -compose copy_opacity -composite -alpha on -alpha background -alpha deactivate\
	 -auto-level -function polynomial 3.5,-5.05,2.05,0.25 -sigmoidal-contrast 0x50% -alpha on ')'\
	 -compose over -compose Hardlight -composite\
	 '(' -clone 0 -background black -shadow 25x2+0+0 -channel A -level 0,50% +channel ')'\
	  +swap +repage -gravity center -compose over -composite cnbc_1.jpg
I'm trying to convert above command into C API. Below is the C code for that. As some of the parts I haven't got it, the code has not completed yet. Mostly I have got a confusion when I need to merge image using -composite also not understood copy_opacity part. Also I attched the images below.

cnbc_i.jpg
https://drive.google.com/file/d/0B-HZjm ... sp=sharing
cnbc_p.jpg
https://drive.google.com/file/d/0B-HZjm ... sp=sharing
cnbc_r.jpg
https://drive.google.com/file/d/0B-HZjm ... sp=sharing
cnbc_1.jpg
https://drive.google.com/file/d/0B-HZjm ... sp=sharing

Code: Select all

#include "MagickWand/studio.h"
#include "MagickWand/MagickWand.h"
void test() {
	/* convert cnbc_r.jpg \
	 '(' cnbc_i.jpg +transparent '#0A0B0D' -alpha extract ')'\
	 '(' cnbc_p.jpg -rotate -33.75 +repage -gravity center -crop 600x437+0+0 +repage ')'\
	 '(' -clone 0 -clone 2 -compose softlight -composite ')'\
	 -delete 0,2 +swap -compose over -alpha off -compose copy_opacity -composite\
	 '(' +clone -alpha extract -write mpr:alpha -blur 0x4 -shade 130x30 mpr:alpha -alpha off\
	 -compose copy_opacity -composite -alpha on -alpha background -alpha deactivate\
	 -auto-level -function polynomial 3.5,-5.05,2.05,0.25 -sigmoidal-contrast 0x50% -alpha on ')'\
	 -compose over -compose Hardlight -composite\
	 '(' -clone 0 -background black -shadow 25x2+0+0 -channel A -level 0,50% +channel ')'\
	  +swap +repage -gravity center -compose over -composite cnbc_1.jpg */


		MagickWand * wand1, * wand2, * wand3, * wand4, * wandc0, * wandc1, * wandc2, * wandc3, * wandc4, * wandc5;
		PixelWand *PW1, * PW2, * pwand;

		PW1 = NewPixelWand();
		PW2 = NewPixelWand();
		pwand = NewPixelWand();

		wand1 = NewMagickWand();
		wand2 = NewMagickWand();
		wand3 = NewMagickWand();

		MagickReadImage(wand1, "cnbc_r.jpg");
		MagickReadImage(wand2, "cnbc_i.jpg");
		MagickReadImage(wand3, "cnbc_p.jpg");


		// (' cnbc_i.jpg +transparent '#0A0B0D' -alpha extract ')'
		PixelSetColor(PW1,"#0A0B0D");
		MagickTransparentPaintImage(wand2, PW1, 1.0, 0, MagickFalse);
		MagickSetImageAlphaChannel(wand2, ExtractAlphaChannel);


		// '(' cnbc_p.jpg -rotate -33.75 +repage -gravity center -crop 600x437+0+0 +repage ')'
		MagickRotateImage(wand3,pwand,-33.75);
		MagickSetImageGravity(wand3, CenterGravity);
		MagickCropImage(wand3, 600, 437, 0, 0);
		MagickResetImagePage(wand3,"600x437+0+0");

		// '(' -clone 0 -clone 2 -compose softlight -composite ')'
		wandc0 = CloneMagickWand(wand1);
		wandc1 = CloneMagickWand(wand3);
		wandc2 = CloneMagickWand(wand1);
		wandc5 = CloneMagickWand(wand1);
		MagickCompositeImage(wandc0, wandc1, SoftLightCompositeOp,MagickFalse, 0, 0);

		// 	 -delete 0,2 +swap -compose over -alpha off -compose copy_opacity -composite
		MagickCompositeImage(wand1, wand2, OverCompositeOp,MagickFalse, 0, 0);
		MagickCompositeImage(wand1, wand3, OverCompositeOp,MagickFalse, 0, 0);
		MagickCompositeImage(wand1, wandc0, OverCompositeOp,MagickFalse, 0, 0);
		MagickSetImageAlphaChannel(wand1, OffAlphaChannel);

		// '(' +clone -alpha extract -write mpr:alpha -blur 0x4 -shade 130x30 mpr:alpha -alpha off
		// -compose copy_opacity -composite -alpha on -alpha background -alpha deactivate
		// -auto-level -function polynomial 3.5,-5.05,2.05,0.25 -sigmoidal-contrast 0x50% -alpha on ')'
		wandc3 = CloneMagickWand(wand1);
		MagickSetImageAlphaChannel(wandc3, ExtractAlphaChannel);
		wandc4 = CloneMagickWand(wandc3);
		MagickBlurImage(wandc4, 4, 0);
		MagickShadeImage(wandc4, MagickTrue, 130, 130);
		MagickSetImageAlphaChannel(wandc3, OffAlphaChannel);
		MagickCompositeImage(wandc3, wandc4, 0,MagickFalse, 0, 0);
		MagickSetImageAlphaChannel(wandc3, OnAlphaChannel);
		MagickSetImageAlphaChannel(wandc3, BackgroundAlphaChannel);
		MagickSetImageAlphaChannel(wandc3, DeactivateAlphaChannel);
		MagickAutoLevelImage(wandc3);
		const double arguments[4] = {3.5,-5.05,2.05,0.25};
		MagickFunctionImage(wandc3, PolynomialFunction, 4, arguments);
		MagickSigmoidalContrastImage(wandc3, MagickTrue, 50.0, 0.0);
		MagickSetImageAlphaChannel(wandc3, OnAlphaChannel);

		// -compose over -compose Hardlight -composite
		MagickCompositeImage(wand1, wand2, OverCompositeOp,MagickFalse, 0, 0);
		MagickCompositeImage(wand1, wand3, OverCompositeOp,MagickFalse, 0, 0);
		MagickCompositeImage(wand1, wandc0, OverCompositeOp,MagickFalse, 0, 0);
		MagickCompositeImage(wand1, wandc3, OverCompositeOp,MagickFalse, 0, 0);

		//'(' -clone 0 -background black -shadow 25x2+0+0 -channel A -level 0,50% +channel ')'
		PixelSetColor(PW2,"#000000");
		MagickSetImageBackgroundColor(wandc5, PW2);
		MagickShadowImage(wandc5, 0, 0, 25, 25);
		MagickSetImageChannelMask(wandc5, AlphaChannel);
		MagickLevelImage(wandc5, QuantumRange/2, 1.0, 0);

		// +swap +repage -gravity center -compose over -composite ./cnbc__1.jpg
		MagickResetImagePage(wand1,"600x437+0+0");
		MagickSetImageGravity(wand1, CenterGravity);
		MagickCompositeImage(wand1, wandc5, OverCompositeOp,MagickTrue, 0, 0);
		MagickWriteImage(wand1,"finalCnbc.jpg");

		DestroyMagickWand(wand1);
		DestroyMagickWand(wand2);
		DestroyMagickWand(wand3);
		DestroyMagickWand(wandc0);
		DestroyMagickWand(wandc1);
		DestroyMagickWand(wandc2);
		DestroyMagickWand(wandc3);
		DestroyMagickWand(wandc4);
		DestroyMagickWand(wandc5);

		DestroyPixelWand(PW1);
		DestroyPixelWand(PW2);
		DestroyPixelWand(pwand);

		MagickWandTerminus();
}
Last edited by rpatelob on 2017-04-25T02:33:10-07:00, edited 1 time in total.

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: MagickWand C API equivalent to below command.

Post by snibgo » 2017-04-24T07:26:47-07:00

As this question is about MagickWand, I'm moving it to that forum.

You should ALWAYS state what version of IM you are using.
snibgo's IM pages: im.snibgo.com

el_supremo
Posts: 1017
Joined: 2005-03-21T21:16:57-07:00

Re: MagickWand C API equivalent to below command.

Post by el_supremo » 2017-04-24T16:40:14-07:00

If you output the command line result to a PNG, it has a transparent background. Even so, it seems to be a very complicated command which seems to just invert the black and white in cnbc_i.jpg, make the black transparent and replace the white area with the pattern in cnbc_p.jpg. Is it supposed to do more than that?

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: MagickWand C API equivalent to below command.

Post by rpatelob » 2017-04-24T23:28:00-07:00

el_supremo wrote:
2017-04-24T16:40:14-07:00
If you output the command line result to a PNG, it has a transparent background. Even so, it seems to be a very complicated command which seems to just invert the black and white in cnbc_i.jpg, make the black transparent and replace the white area with the pattern in cnbc_p.jpg. Is it supposed to do more than that?

Pete
cnbc_i.jpg is just in a black and white but if the image has multiple colors, I'll find top most colors. let's say there is a image of 8 top most colors. I run that command that I have provided 8 times with different colors and finally flatten all images together and make one single image. Check below image.
https://drive.google.com/file/d/0B-HZjm ... sp=sharing

rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: MagickWand C API equivalent to below command.

Post by rpatelob » 2017-04-24T23:54:00-07:00

rpatelob wrote:
2017-04-24T23:28:00-07:00
el_supremo wrote:
2017-04-24T16:40:14-07:00
If you output the command line result to a PNG, it has a transparent background. Even so, it seems to be a very complicated command which seems to just invert the black and white in cnbc_i.jpg, make the black transparent and replace the white area with the pattern in cnbc_p.jpg. Is it supposed to do more than that?

Pete
cnbc_i.jpg is just in a black and white but if the image has multiple colors, I'll find top most colors. let's say there is a image of 8 top most colors. I run that command that I have provided 8 times with different colors and finally flatten all images together and make one single image. Check below image.
https://drive.google.com/file/d/0B-HZjm ... sp=sharing
Let's pic the green color, Next I need to generate the image like below using convert cnbc.jpg +transparent '#00A13A' -alpha extract test1.jpg command.
https://drive.google.com/file/d/0B-HZjm ... sp=sharing.

And after that I need to execute complex command I have provided in description of my question I will get the image like below.
https://drive.google.com/file/d/0B-HZjm ... sp=sharing

So I will generate 8 different images using the same procedure and flatten them into one image.

rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: MagickWand C API equivalent to below command.

Post by rpatelob » 2017-04-25T02:42:16-07:00

rpatelob wrote:
2017-04-24T06:48:22-07:00

Code: Select all

convert cnbc_r.jpg \
	 '(' cnbc_i.jpg +transparent '#0A0B0D' -alpha extract ')'\
	 '(' cnbc_p.jpg -rotate -33.75 +repage -gravity center -crop 600x437+0+0 +repage ')'\
	 '(' -clone 0 -clone 2 -compose softlight -composite ')'\
	 -delete 0,2 +swap -compose over -alpha off -compose copy_opacity -composite\
	 '(' +clone -alpha extract -write mpr:alpha -blur 0x4 -shade 130x30 mpr:alpha -alpha off\
	 -compose copy_opacity -composite -alpha on -alpha background -alpha deactivate\
	 -auto-level -function polynomial 3.5,-5.05,2.05,0.25 -sigmoidal-contrast 0x50% -alpha on ')'\
	 -compose over -compose Hardlight -composite\
	 '(' -clone 0 -background black -shadow 25x2+0+0 -channel A -level 0,50% +channel ')'\
	  +swap +repage -gravity center -compose over -composite cnbc_1.jpg

From above command I haven't understood -delete 0,2 +swap -compose over -alpha off -compose copy_opacity -composite part. what is copy_opacity and composite on how my images?

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: MagickWand C API equivalent to below command.

Post by fmw42 » 2017-04-25T11:16:48-07:00

-delete 0,2 removes two temporary created images when they are no longer needed.

+swap swaps the order of the last two images in the sequence

-compose over -alpha off -compose copy_opacity puts the last image into the alpha channel of the previous image

I would suggest that you put +write tmpX.png at various places in the command line to see what it is generating. That often helps understand the processing. X=0,1,2....

Post Reply