Page 1 of 1

Trying to add stroke on caption in Wand (Python binding)

Posted: 2018-08-27T05:18:59-07:00
by Ivan Holmes
I'm not really sure if this is the correct place for this question, but as Wand relies on MagickWand, I assume that it is at least relevant here. If not, please move it.

Using ImageMagick (6.9.10-10) via the command line, I am able to define a stroke width and colour for a caption like so:

Code: Select all

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 is what the resultant image looks like:
Image

I am trying to extend Wand to allow this functionality. The code for adding a caption is located in image.py in the Wand library. This file can be found here: Wand GitHub repo.

As far as I can tell, the pertinent piece of code is this:

Code: Select all

        with Image() as textboard:
            library.MagickSetSize(textboard.wand, width, height)
            textboard.font = font
            textboard.gravity = gravity or self.gravity
            with Color('transparent') as background_color:
                library.MagickSetBackgroundColor(textboard.wand,
                                                 background_color.resource)
            textboard.read(filename=b'caption:' + text.encode('utf-8'))
            self.composite(textboard, left, top)
I have so far tried to extend this code using the functions DrawSetStrokeColor and DrawSetStrokeWidth (thanks fmw42). The colour and width of the stroke are arbitrary at this point.

Code: Select all

        with Image() as textboard:
            library.MagickSetSize(textboard.wand, width, height)
            textboard.font = font
            textboard.gravity = gravity or self.gravity
            with Color('transparent') as background_color:
                library.MagickSetBackgroundColor(textboard.wand,
                                                 background_color.resource)
            with Color('black') as stroke_color:
            	library.DrawSetStrokeColor(textboard.wand, stroke_color.resource)
            library.DrawSetStrokeWidth(textboard.wand, 2)
            textboard.read(filename=b'caption:' + text.encode('utf-8'))
            self.composite(textboard, left, top)
This code fails to run, giving this error:

Code: Select all

Assertion failed: (wand->signature == WandSignature), function DrawSetStrokeColor, file wand/drawing-wand.c, line 5306.
I am pretty sure this is because I am not performing the draw operations on a DrawingWand. I have tried to create one, but get an error, and I have also tried to create an instance of the Drawing() class in Wand, but this fails because drawing.py imports image.py, so I cannot import drawing.py from image.py.

I would be really grateful if someone could hint to me a viable way to solve this problem. I'm not a particularly strong programmer and I feel like I am banging my head against a wall!

Thanks,
Ivan

Re: Trying to add stroke on caption in Wand (Python binding)

Posted: 2018-08-30T19:04:40-07:00
by Ivan Holmes
Hi,
I found an answer to this issue on StackOverflow.

Thanks