Annotate autosize

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
walzmyn

Annotate autosize

Post by walzmyn »

The IM command line command

Code: Select all

convert -size 300x300 xc:white -fill black -gravity center label:MakeThisWork example.png
will make a white image with the text "MakeThisWork" across it in black. The magick piece here is if you don't give the label a pointsize it will autosize the text to fit inside the image.

Is there away to make this auto-sizing bit work in perlmagick? I've experimented with a whole bunch of options, but not been successful, it seems to really want to have a pointsize given to it.

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

Re: Annotate autosize

Post by fmw42 »

I don't know PerlMagick, but you can try to give it just a width or height (and no pointsize) and that may then allow it to autosize to fill the width or height.

see command line examples at http://www.imagemagick.org/Usage/text/#label_bestfit
walzmyn

Re: Annotate autosize

Post by walzmyn »

Yeah, that's what the line I used as an example does. I can get a standard IM command to do exactly what I want, I can't get perlmagick to emulate.
I've tried passing it a 'size' argument, making the pointsize 'auto' and the geometry argument. But anything with out a pointsize seems to just give you a default size, not scaled to the containing image.

By the way, after running several tests, I don't know what the geometry argument does either :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Annotate autosize

Post by fmw42 »

I believe the -geometry argument allows you to offset the text anywhere you want rather than using -gravity to just center or put in corners (or in combination to -gravity so that you can position offsets for example relative to -gravity center or some other -gravity position such as the bottom)

I think you will need a PerlMagick expert to help on the auto issue.

Did you try supplying only one dimension -- either width or height -- rather than both?
walzmyn

Re: Annotate autosize

Post by walzmyn »

Yes I did. I've tried dozens of combinations, no love.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Annotate autosize

Post by anthony »

As you never gave an example of WHAT you tried , I can assume that the subject is what you are attempting to do. Bestfit annotation. That is NOT posible!

Bestfit is for the label: image generation - it has nothing to to with 'annotate' which is a much lower level image drawing operator requiring an existing canvas! annotate does not have best fit capability.

The code to 'bestfit' is built into the coder for "label:" itself. As such it should be independent on the actual API used. The key is to ensure that the 'size' parameter is passed on to the call to readImage "label:" Specifically non-zero size and a zero (undefined) pointsize.

This worked for me!!!

Code: Select all

use Image::Magick;
$label=Image::Magick->new(size=>"600x600");
$label->Read("label:testing");
$label->Write("test.png");
What label does is attempts multiple pointsizes until it finds one that JUST fits inside the bounds specified. See "coders/label.c" and ReadLABELImage(). You should be able to follow the code as a programmer, even if you are not specifically a C programmer.

Actually this is the first time I even looked at that specific code myself.
It doubles the pointsize (starting at the default 12) until font is too big,
Then tried subtracting one until the text bound does fit.
After it has the right pointsize it carries on normal, creating the images canvas, handling gravity, and annotating the image onto the canvas. Oh and it reports the resulting point size it decided on in a Image Properity (setting) so you can look it up.

If you have an existing canvas and want to do 'bestfit' annotation (not image creation), then you will need to DIY it. However it is probably easier to create a bestfit label, and then compose that label onto the existing canvas. Slightly slower, but definitely easier.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
walzmyn

Re: Annotate autosize

Post by walzmyn »

You're awesome.
I did not realize you could use the label:text inside perl::magick.
I'm not really a coder, I'm just hacking some stuff together to make my life easier for some items I repeat often. In this case I'm watermarking pictures with text that is different each time.
Thank you.
Post Reply