Need help re-writing convert command to Wand API

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
RobertAnthony

Need help re-writing convert command to Wand API

Post by RobertAnthony »

Wand Gurus,

Is there a straightforward way of converting the following command into wand API equivalent ?

convert -background none -gravity center -fill black -font Arial -size 170x170 -rotate -5 -stroke none caption:Hello +size background_image.jpg +swap -gravity northwest -geometry +0+0 -composite out.jpg

Regards,
-Robert
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Need help re-writing convert command to Wand API

Post by magick »

Most command line options have a Wand analog. For example, -rotate translates to
  • MagickRotateImage( wand, 5.0 );
To read an image use MagickReadImage(). To set a font, use MagickSetFont(). Have fun.
RobertAnthony

Re: Need help re-writing convert command to Wand API

Post by RobertAnthony »

magick wrote:Most command line options have a Wand analog. For example, -rotate translates to
  • MagickRotateImage( wand, 5.0 );
To read an image use MagickReadImage(). To set a font, use MagickSetFont(). Have fun.
Thanks. That's a start. I have managed to get a little further, but I am still not able to get the same result.

The convert command I am trying to implement can be broken down into:
1) Create blank image, size=170x170, background = none, gravity=center, caption="Hello", font=XYZ, rotation=-5, disable sizing of caption so that text size is maximized, set stroke
2) Read existing image, set gravity to NorthWest, offset 0+0
3) Compose 1 over 2 (so I guess swap should happen before compose?)
4) Write image out

What is the best way to create an image using auto-sized caption ?
RobertAnthony

Re: Need help re-writing convert command to Wand API

Post by RobertAnthony »

I think I finally got it. The code also passes the thread test, which makes it a lot better alternative. The only problem remaining is that the text is not centered.

What can be done to the Magick wand so that the caption that is read is is centered ?

Code: Select all

MagickWandGenesis();  

  wand=NewMagickWand();
  bgwand=NewMagickWand();
  MagickSetBackgroundColor(wand, c_none);
  MagickSetFont(wand, font);
  MagickSetSize(wand, text_width, text_height);
  MagickReadImage(wand, "caption:This should be centered");  
  MagickRotateImage(wand, c_none, -5.0);
  MagickReadImage(bgwand, "bg.jpg");
  MagickCompositeImage(bgwand, wand, OverCompositeOp, 10, 10);
  MagickWriteImages(bgwand, argv[24], MagickTrue);      

   // Cleanup...

  MagickWandTerminus();
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Need help re-writing convert command to Wand API

Post by magick »

We will add a new method, MagickSetGravity(), to set the image gravity. In the mean-time use MagickSetOption(wand,"gravity","center") before the image is read.
RobertAnthony

Re: Need help re-writing convert command to Wand API

Post by RobertAnthony »

magick wrote:We will add a new method, MagickSetGravity(), to set the image gravity. In the mean-time use MagickSetOption(wand,"gravity","center") before the image is read.
That worked! Thanks for all your help.
Post Reply