Draw Text to fit a size

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
Costyv95
Posts: 6
Joined: 2016-03-24T06:11:41-07:00
Authentication code: 1151

Draw Text to fit a size

Post by Costyv95 »

Hello. I need to draw a text and make it fit a certain size(300x500). I searched on google and found only terminal commands to do it , but no answer regarding magick++ . I use drawableText, but i don't know what drawable to use in order to force the text to fit a certain rectangle.


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

Re: Draw Text to fit a size

Post by snibgo »

I don't know the Magick++ API. At the MagickCore level, it can be done like this:

Code: Select all

  image_info=CloneImageInfo((ImageInfo *) NULL);
  image_info->size = ConstantString ("300x500");
  (void) strcpy(image_info->filename,"label:Hello");
  images=ReadImage(image_info,exception);
snibgo's IM pages: im.snibgo.com
Costyv95
Posts: 6
Joined: 2016-03-24T06:11:41-07:00
Authentication code: 1151

Re: Draw Text to fit a size

Post by Costyv95 »

Thank You, I've reached the label solution, but I have another question. How can I change the color of the label text? fillColor for that image didn't work for me..
Costyv95
Posts: 6
Joined: 2016-03-24T06:11:41-07:00
Authentication code: 1151

Re: Draw Text to fit a size

Post by Costyv95 »

snibgo wrote:I don't know the Magick++ API. At the MagickCore level, it can be done like this:

Code: Select all

  image_info=CloneImageInfo((ImageInfo *) NULL);
  image_info->size = ConstantString ("300x500");
  (void) strcpy(image_info->filename,"label:Hello);
  images=ReadImage(image_info,exception);

Thank You, I've reached the label solution, but I have another question. How can I change the color of the label text? fillColor for that image didn't work for me..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Draw Text to fit a size

Post by snibgo »

Setting the fill colour after the image is created won't work. To fill with, say, Red:

Code: Select all

  image_info=CloneImageInfo((ImageInfo *) NULL);
  image_info->size = ConstantString ("300x500");
  SetImageOption (image_info, "fill", "Red");
  (void) strcpy(image_info->filename,"label:hello");
  images=ReadImage(image_info,exception);
snibgo's IM pages: im.snibgo.com
Costyv95
Posts: 6
Joined: 2016-03-24T06:11:41-07:00
Authentication code: 1151

Re: Draw Text to fit a size

Post by Costyv95 »

Thank You, I'll try to translate that to magick++ .
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Draw Text to fit a size

Post by snibgo »

When you find the answer, please post a code snippet here, to help others.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Draw Text to fit a size

Post by snibgo »

Looking at Magick++\options.h, method "void fillColor(const Color &fillColor_)" looks likely.
snibgo's IM pages: im.snibgo.com
Costyv95
Posts: 6
Joined: 2016-03-24T06:11:41-07:00
Authentication code: 1151

Re: Draw Text to fit a size

Post by Costyv95 »

snibgo wrote:Looking at Magick++\options.h, method "void fillColor(const Color &fillColor_)" looks likely.
Options.h is a internal class which i cannot use in my code. For the moment I have this code:

Code: Select all

 
 		 Image img;
        img.fillColor("red"); // this has no effect for the label, only for drawable objects that i draw when i use method img.draw()
        img.size(Geometry(300,300));
        img.backgroundColor("transparent");
        img.font("Liberation-Sans-Regular");
        string text = "label:" + "This is a test";
        img.read(text);
 
I still think that this solution is not the best approach to make a text fit a size and still have no idea how to change the color of the label or allign it(centered for example).
Costyv95
Posts: 6
Joined: 2016-03-24T06:11:41-07:00
Authentication code: 1151

Re: Draw Text to fit a size

Post by Costyv95 »

The code i wrote earlier works fine. It didn't work at me because I wasn't using the last version of Magick++ , but libmagick++-dev . After I downloaded the last version of ImageMagick from git and compile it, both img.fillColor(Color("red")) and img.textGravity(CenterGravity); worked.

The last code is:

Code: Select all

	 Image img;
    img.textGravity(CenterGravity);
    img.fillColor(Color("blue"));
    img.size(Geometry(300,300,0,0));
    img.backgroundColor("red");
    string text = "label:";
    text += "This is a test";
    img.read(text);
    img.write("output.png");
Post Reply