Text Effects with MagickWand

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
Prince09

Text Effects with MagickWand

Post by Prince09 »

Hello,

I need to use a few text effects while annotating text in such as Shadows, Gradients (I see that these can be done from the command line) and the like. I'm not quite sure which functions in the MagickWand API (for C) support such operations. I've come across DrawSetTextDecoration but looks like it supports only LineThrough/Underline.

Can anyone point me in the right direction to do this? Any help will be greatly appreciated.
kustom

Re: Text Effects with MagickWand

Post by kustom »

hi prince,

actually i have the same problem, i dont know exactly if you can do the same effects using Wand API than using command-line ImageMagick :(

im trying these days, i will inform you if i acquire some info....

regards

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

Re: Text Effects with MagickWand

Post by el_supremo »

I've been playing around with text effects and here's some code to create these two images:

http://members.shaw.ca/el_supremo/text_pattern.png
http://members.shaw.ca/el_supremo/text_shadow.png

Pete

Code: Select all

#include <windows.h>
#include <wand/magick_wand.h>

// The pattern_name MUST have a leading #
void set_tile_pattern(DrawingWand *d_wand,char *pattern_name,char *pattern_file)
{
	MagickWand *t_wand;
	long w,h;

	t_wand=NewMagickWand();  
	MagickReadImage(t_wand,pattern_file);
	// Read the tile's width and height
	w = MagickGetImageWidth(t_wand);
	h = MagickGetImageHeight(t_wand);
	DrawPushPattern(d_wand, pattern_name+1, 0, 0, w, h);
	DrawComposite(d_wand, SrcOverCompositeOp, 0, 0, 0, 0, t_wand);
	DrawPopPattern(d_wand);
	DrawSetFillPatternURL(d_wand, pattern_name);
}
void test_wand(LPTSTR lpCmdLine)
{
	MagickWand *magick_wand = NULL;
	MagickWand *c_wand = NULL;
	DrawingWand *d_wand = NULL;
	PixelWand *p_wand = NULL;


	MagickWandGenesis();


// Text effect 1 - shadow effect using MagickShadowImage
	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();
	PixelSetColor(p_wand,"none");
	// Create a new transparent image
	MagickNewImage(magick_wand,640,480,p_wand);

	// Set up a 72 point white font 
	PixelSetColor(p_wand,"white");
	DrawSetFillColor(d_wand,p_wand);
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Add a black outline to the text
	PixelSetColor(p_wand,"black");
	DrawSetStrokeColor(d_wand,p_wand);
	// Turn antialias on - not sure this makes a difference
	DrawSetTextAntialias(d_wand,MagickTrue);
	// Now draw the text
	DrawAnnotation(d_wand,200,140,"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);

	// Trim the image down to include only the text
	MagickTrimImage(magick_wand,0);
	// equivalent to the command line +repage
	MagickResetImagePage(magick_wand,"");

	// Make a copy of the text image
	c_wand = CloneMagickWand(magick_wand);

	// Set the background colour to blue for the shadow
	PixelSetColor(p_wand,"blue");
	MagickSetImageBackgroundColor(magick_wand,p_wand);

	// Opacity is a real number indicating (apparently) percentage
	MagickShadowImage(magick_wand,79,4,5,5);
	
	// Composite the text on top of the shadow
	MagickCompositeImage(magick_wand,c_wand,OverCompositeOp,5,5);

	// Create a new image the same size as the text image and put a solid colour
	// as its background
	PixelSetColor(p_wand,"rgb(125,215,255)");
	if(c_wand)c_wand = DestroyMagickWand(c_wand);
	c_wand = NewMagickWand();
	MagickNewImage(c_wand,MagickGetImageWidth(magick_wand),MagickGetImageHeight(magick_wand),p_wand);
	// Now composite the shadowed text over the plain background
	MagickCompositeImage(c_wand,magick_wand,OverCompositeOp,0,0);
	// and write the result
	MagickWriteImage(c_wand,"text_shadow.png"); 

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(c_wand)c_wand = DestroyMagickWand(c_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);

// Text effect 2 - patterned text
	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();

	set_tile_pattern(d_wand,"#check","pattern:checkerboard");

	PixelSetColor(p_wand,"none");
	// Create a new transparent image
	MagickNewImage(magick_wand,640,480,p_wand);

	// Set up a 72 point font 
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Now draw the text
	DrawAnnotation(d_wand,200,140,"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);
	// Trim the image
	MagickTrimImage(magick_wand,0);
	// Add a white border
	PixelSetColor(p_wand,"white");
	MagickBorderImage(magick_wand,p_wand,5,5);
	// and write it
	MagickWriteImage(magick_wand,"text_pattern.png");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);

	MagickWandTerminus();
}
kustom

Re: Text Effects with MagickWand

Post by kustom »

Great!

seeing your code it seems like you can do lot of interesting effects using this API, i think using MagickSetOption() function you can use Wand very common to command-line imagemagick...


now im trying to do the "-distort arc" effect, but i can get any result :(


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

Re: Text Effects with MagickWand

Post by el_supremo »

I've added an arc effect to my code to produce this image:
http://members.shaw.ca/el_supremo/text_arc.png
One thing to be aware of is that arc appears to rotate around the middle of the image so if you want a symmetric arc like this one, the text must be centred in the image. Change the dimensions of MagickNewImage to 640,480 and you'll see what I mean in the result.

First, add this decalration at the head of the test_wand function:

Code: Select all

	// Used for text effect #3
	double dargs[1] = {120.};
and then add this code immediately before the MagickWandTerminus();

Code: Select all

// Text effect 3 -  arc font 
//convert -size 320x100 xc:lightblue -font Candice -pointsize 72 \
//           -annotate +25+65 'Anthony' -distort Arc 120 \
//           -trim +repage -bordercolor lightblue -border 10  font_arc.jpg
	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();

	// Create a 320x100 lightblue canvas
	PixelSetColor(p_wand,"lightblue");
	MagickNewImage(magick_wand,320,100,p_wand);

	// Set up a 72 point font 
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Now draw the text
	DrawAnnotation(d_wand,25,65,"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);

	MagickDistortImage(magick_wand,ArcDistortion,1,dargs,MagickFalse);

	// Trim the image
	MagickTrimImage(magick_wand,0);
	// Add the border
	PixelSetColor(p_wand,"lightblue");
	MagickBorderImage(magick_wand,p_wand,10,10);

	// and write it
	MagickWriteImage(magick_wand,"text_arc.png");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);
Pete
kustom

Re: Text Effects with MagickWand

Post by kustom »

thanks a lot of pete!

i dont knew this function exist :o

MagickDistortImage()


i thought all this effect should be made invoking this function like:

i.e:
...
MagickSetOption(wand,"gravity","center");
MagickSetOption(wand,"stroke","black");
...


fine to know people like you :)
Prince09

Re: Text Effects with MagickWand

Post by Prince09 »

hey guys ... thanks a ton!! that was really helpful!! :-)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Text Effects with MagickWand

Post by anthony »

Stay tuned to IM Examples, Distortions, the Barrel (and Pincussion) distortion is available and working for the next release (IM v6.4.3) and as this effect will allow separate X and Y values you will be able to generate pinched and bulging text styles, similar to effects currently shown (not with text) on Fred Weinhaus's IM script 'pinbarrel'. http://www.fmwconcepts.com/imagemagick/ ... /index.php
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Text Effects with MagickWand

Post by anthony »

el_supremo wrote:One thing to be aware of is that arc appears to rotate around the middle of the image so if you want a symmetric arc like this one, the text must be centred in the image.
Actually Arc converts the top edge of the image into the outer part of the circle. There are arguments in Arc to specify at what angle the 'center' of the given image should lie on that circle.

Remember all distortions deals with images not text!

In the example output you gave the text is slanted, when when arced caused all the slants to become spirals, making it look very strange.

It is also a simplification of the 'Polar' distortion that was added in the IM releases of just a few days ago. However Arc is designed to try an preserve the aspect ratio as much as posible, where Polar is designed more for full circle and full image distorting.

Barrel Distortions are present in the latest release too, I just haven't written up its documentation yet.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Text Effects with MagickWand

Post by fmw42 »

You are welcome to review and convert any part of my texteffect command line script that uses barrel/pincussion, perspective, arc, etc distortions WITH text. Unfortunately, I cannot help as I don't know this API.

http://www.fmwconcepts.com/imagemagick/index.html

I have not yet converted the script to use Anthony's new -distort barrel. It still uses -fx for this effect to do pinch/bulge.
Post Reply