"convert"-type function in C++? (font to image)

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
NetJohn
Posts: 8
Joined: 2016-09-24T21:02:07-07:00
Authentication code: 1151

"convert"-type function in C++? (font to image)

Post by NetJohn »

I was looking at here: http://www.imagemagick.org/Usage/annotating/

Down in the "Watermarking with Text" section, it shows the use of the convert commandline tool to add text to an image.

There doesn't seem to be a convert API, so I'm guessing this is a compilation of a lot of other stuff.

Basically, looking for pointers on C++ way to do things here: http://www.imagemagick.org/Usage/fonts/

Thanks,
John
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "convert"-type function in C++? (font to image)

Post by fmw42 »

I do not use C++ or Magick++, but you might find what you want at:

http://www.imagemagick.org/Magick++/
http://www.imagemagick.org/Magick++/Documentation.html
NetJohn
Posts: 8
Joined: 2016-09-24T21:02:07-07:00
Authentication code: 1151

Re: "convert"-type function in C++? (font to image)

Post by NetJohn »

fmw42,

Thank you for your quick reply.

I've been all throughout the Documentation and the API, and there is not anything similar to convert in the code. I have the Image class webpage up as a constant source of information. It covers pretty much everything else (and then some), but I didn't see anything regarding convert or applying text to an image.

This is posted in the Magick++ forum to narrow the audience (rather than the general User forum).

John
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "convert"-type function in C++? (font to image)

Post by fmw42 »

I do not think there is a convert equivalent. You create a new instance of the magick++, read the file, then just use the operator/methods to process, then write the file. Something like that. Sorry for my ignorance. But someone who knows magick++ will probably answer tomorrow.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: "convert"-type function in C++? (font to image)

Post by snibgo »

I'm not sure what you want. "convert" is a general-purpose program, that does what you tell it to do by program arguments.

If you want a C function for the same thing, that would be ConvertImageCommand(). (There may be a C++ wrapper for this.) So a complete program that replicates the v6 convert command is:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <wand/MagickWand.h>

int main (int argc, char **argv)
{
  MagickBooleanType
    status;

  ExceptionInfo * exception = AcquireExceptionInfo ();
  ImageInfo * image_info = AcquireImageInfo ();

  status = ConvertImageCommand(image_info, argc, argv, NULL, exception);
  if (status == MagickFalse)
    MagickError(exception->severity,exception->reason,exception->description);

  image_info = DestroyImageInfo (image_info);
  exception = DestroyExceptionInfo (exception);

  return (status==MagickFalse) ? 1 : 0;
}
If you want something that isn't general-purpose, but does a single job like annotating an image with text, then MagickAnnotateImage() will do that.
snibgo's IM pages: im.snibgo.com
NetJohn
Posts: 8
Joined: 2016-09-24T21:02:07-07:00
Authentication code: 1151

Re: "convert"-type function in C++? (font to image)

Post by NetJohn »

snibgo,

Take a look here: http://www.imagemagick.org/Usage/fonts/

First item, it says to do the following:

Code: Select all

   convert -size 320x100 xc:lightblue -font Candice -pointsize 72 \
           -tile pattern:checkerboard   -annotate +28+68 'Anthony' \
           font_tile.jpg
Looking to do that via the Magick++ library, there is no class component called "convert". There is "annotate", but it takes a string for the text with no capability of setting up font, pointsize, etc.

I'm looking to develop software that can open an image, resize it, print on it, then save it. I can do that with annotate, but only the text and gravity can be set. Unfortunately, most aspects of IM examples use commandline calls, not Magick++ calls.

I've been looking more into the API and it looks like the Drawable class (http://www.imagemagick.org/Magick++/Drawable.html) might be what I want, as I can set font, pointsize, etc. Now trying to figure out if that's right or not.

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

Re: "convert"-type function in C++? (font to image)

Post by snibgo »

For Magick++, the first place to look is generally Magick::Image Class, http://www.imagemagick.org/Magick++/Image++.html

Near the top are examples showing how to read a file, or create an image of given size and colour, and save to a file..

Then you want the equivalents of "-font", "-pointsize", "-tile" and "-annotate". If you search for those words on that page, you'll soon find what you need.
snibgo's IM pages: im.snibgo.com
NetJohn
Posts: 8
Joined: 2016-09-24T21:02:07-07:00
Authentication code: 1151

Re: "convert"-type function in C++? (font to image)

Post by NetJohn »

Got things working... mostly. For those who are paying attention, my code is as follows:

Code: Select all

	// Blank canvas to put text on
	Magick::Image * outimage = new Magick::Image(Magick::Geometry(mmw, mmh, 0, 0), Magick::Color("black"));
	// Values of mmw and mmh defined elsewhere previous
	
	// Other stuff done
	outimage->font(context.marshal_as<std::string>(FontDialog->Font->Name)); // THIS WORKS RARELY
	outimage->fontPointsize(FontDialog->Font->SizeInPoints);
	outimage->fillColor(Magick::Color("white")); // CAN'T GET THE COLOR FROM THE DIALOG, WON'T WORK

	// Other stuff done
	outimage->annotate(context.marshal_as<std::string>(sTextName), Magick::Geometry(0, 0, textHoff, textWoff));
	// Values of textHoff and textWoff defined elsewhere previous
	outimage->repage();
In playing around I found that getting the name of the font from the font dialog only works if the dialog returns a name that matches the actual font file name. "Arial" works as long as the font is "arial.otf". Not sure what the issue is with the color, but I'm guessing is similar textual mismatches.

Apparently anything regarding font or fillcolor doesn't have very robust error handling, as the program just dies at that point if it doesn't match (debugging in VS C++ .NET 2015 doesn't work with IM).

I'll update as I find more solutions.

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

Re: "convert"-type function in C++? (font to image)

Post by snibgo »

Thanks for the update.

When IM can't do something, like set a fill colour from a name that doesn't exist, it raises an error. You can catch these errors and take some reasonable action.
snibgo's IM pages: im.snibgo.com
NetJohn
Posts: 8
Joined: 2016-09-24T21:02:07-07:00
Authentication code: 1151

Re: "convert"-type function in C++? (font to image)

Post by NetJohn »

I don't believe I was getting an uncaught exception, but since I stopped causing the issue, I haven't seen it recently and may not recall it correctly. I'll check again and throw a few try/catch around it to see if I can handle.

John
Post Reply