2

ImageMagick lets you apply a caption to an image. The caption text is automatically sized and wrapped to fit into the area that you define.

Using ImageMagick via the command line, I am able to define a stroke width and colour for this caption like so:

convert -size 300x300 -stroke black -strokewidth 1 -fill white \
    -background transparent -gravity center \
    caption:"This is a test of the caption feature in ImageMagick" ~/out.png

This image is the output from the command, for reference.

I cannot find anywhere online how to apply these attributes using the MagickWand C bindings. I can create a caption and change its font and font colour, but I cannot figure out how to add a stroke.

I want to know this information in order to add support for this to the Wand bindings for Python. I would be open to an alternative way to accomplish automatically sized text with gravity and a stroke, but preferably not requiring an inelegant workaround or external software.

As further information, I am using ImageMagick 6.9.10-10 on macOS 10.13.6 installed via Homebrew.

emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Is it not here --- https://www.imagemagick.org/api/drawing-wand.php -- DrawSetStrokeWidth and DrawSetStrokeColor – fmw42 Aug 27 '18 at 00:35
  • CONTINUED: For the future, for other missing features in Python Wand, you might want to ask for help more directly on the Imagemagick forum at https://www.imagemagick.org/discourse-server/ and https://www.imagemagick.org/discourse-server/memberlist.php?mode=contactadmin. Also see https://www.imagemagick.org/script/magick-wand.php – fmw42 Aug 27 '18 at 00:44
  • Hi @fmw42, I recognise your username from the ImageMagick forum. Thanks for illuminating me as to the correct functions, I spent quite a lot of time on the API site but somehow didn't find those. I've run into further issues so I will register on the forum now. – Ivan Holmes Aug 27 '18 at 12:00
  • I've registered and submitted my question. Thanks. – Ivan Holmes Aug 27 '18 at 12:19

1 Answers1

1

Technically you would be responsible for building a drawing-context, and calculate word wrapping. Usually by invoking the MagickQueryMultilineFontMetrics.

However, the caption: protocol was provided as a short-cut. You can review the source code to see how to achieve such calculations, but if your not interested, one can quickly hack a solution with MagickSetOption before invoking the Image Read methods.

With C

#include <wand/MagickWand.h>

int main(int argc, const char * argv[]) {
    MagickWandGenesis();
    MagickWand * wand;
    wand = NewMagickWand();
    // -size 300x300
    MagickSetSize(wand, 300, 300);
    // -stroke black
    MagickSetOption(wand, "stroke", "BLACK");
    // -strokewidth 1
    MagickSetOption(wand, "strokewidth", "1");
    // -fill white
    MagickSetOption(wand, "fill", "WHITE");
    // -background transparent
    MagickSetOption(wand, "background", "TRANSPARENT");
    // -gravity center
    MagickSetGravity(wand, CenterGravity);
    // caption:"This is a test of the caption feature in ImageMagick"
    MagickReadImage(wand, "caption:This is a test of the caption feature in ImageMagick");
    // ~/out.png
    MagickWriteImage(wand, "~/out.png");
    wand = DestroyMagickWand(wand);
    MagickWandTerminus();
    return 0;
}

With wand

from wand.image import Image
from wand.api import library

with Image() as img:
    # -size 300x300
    library.MagickSetSize(img.wand, 300, 300)
    # -stroke black
    library.MagickSetOption(img.wand, b"stroke", b"BLACK")
    # -strokewidth 1
    library.MagickSetOption(img.wand, b"strokewidth", b"1")
    # -fill white
    library.MagickSetOption(img.wand, b"fill", b"WHITE")
    # -background transparent
    library.MagickSetOption(img.wand, b"background", b"TRANSPARENT")
    # -gravity center
    img.gravity = "center"
    # caption:"This is a test of the caption feature in ImageMagick"
    img.read(filename="caption:This is a test of the caption feature in ImageMagick")
    # ~/out.png
    img.save(filename="~/out.png")
fmw42
  • 46,825
  • 10
  • 62
  • 80
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Edited a minor spelling typo in your text. – fmw42 Aug 27 '18 at 16:45
  • Thank you very much for this. I've used the information you supplied to add this into Wand: https://github.com/dahlia/wand/pull/375 My programming ability doesn't really provide for anything beyond the quick solution! – Ivan Holmes Aug 28 '18 at 17:37
  • Just realised you're a pretty big contributor to Wand - nice! – Ivan Holmes Aug 28 '18 at 17:41