0

I've got ImageMagick software installed with homebrew on my mac and I'm using the Image::Magick Perl module. I'm trying to figure out how to annotate images with some text. Nothing I've tried works:

$error = $image->Annotate(font => 'Courier', gravity => 'North', pointsize=>100, fill=>'black', text => 'blah blah');

$error = $image->Annotate(font => '/Library/Fonts/Impact.ttf', gravity => 'North', pointsize=>100, fill=>'black', text => 'blah blah');

I'm figuring it can't find the font but maybe something else is wrong. No error is thrown. I have installed ghostscript with homebrew but that didn't help.

Partial output from identify -list font

  Font: Times-BoldItalic
    family: Times
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n021024l.pfb
  Font: Times-Italic
    family: Times
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n021023l.pfb
  Font: Times-Roman
    family: Times
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n021003l.pfb
StevieD
  • 6,925
  • 2
  • 25
  • 45
  • 1
    See what fonts are installed in Terminal with `identify -list font`. If you have none, see https://stackoverflow.com/a/24701602/2836621 – Mark Setchell Jun 21 '18 at 14:26
  • If you have only Times then it's not going to work if you choose Courier or Impact. – Borodin Jun 21 '18 at 14:42
  • 1
    Maybe try this in Terminal and see how `z.png` looks... `convert -size 100x100 xc:white -gravity center -fill red -font /Library/Fonts/Impact.ttf -annotate 0 "Hi there" z.png` – Mark Setchell Jun 21 '18 at 14:45
  • @Borodin It should work if OP specifies full path to `/Library/Fonts/Impact.ttf` like he suggests he did, even if that font is not configured into ImageMagick's configuration settings. – Mark Setchell Jun 21 '18 at 14:47
  • @Borodin I have hundreds of fonts. that's just a snippet I posted. – StevieD Jun 21 '18 at 14:51
  • @MarkSetchell yup, that worked. hmmm. – StevieD Jun 21 '18 at 14:51
  • 1
    Maybe you are writing black on black? Or the Northern gravity is throwing you off the top of the page? – Mark Setchell Jun 21 '18 at 14:53
  • Nope. Black on mostly white. I tried with an without gravity. Maybe I can just have my perl script run a command line command for the existing image instead. How would that command look? ImageMagick commands confuse the hell out of me. – StevieD Jun 21 '18 at 14:57
  • 1
    Try setting Gravity to `center`, as it defaults to NorthWest even if you don't set it. – Mark Setchell Jun 21 '18 at 14:59
  • Bam! Yup, there it is. – StevieD Jun 21 '18 at 15:01
  • It's weird though, it's not always centered. For some images it is actually appearing near the top, sometimes in the center. It depends on the dimensions of the image. – StevieD Jun 21 '18 at 15:02
  • 1
    Setting it to North or NorthWest makes the top of the image the **bottom** reference point for the text, i.e. the baseline of the text. – Mark Setchell Jun 21 '18 at 15:02
  • Ah, makes sense. So I just need to lower it. I can figure that out. Thanks so much! Saved me probably 2 hours! – StevieD Jun 21 '18 at 15:03
  • 1
    Try running `identify YourImage.png` in the Terminal, and see if there is any +nnn+nnn after the image dimensions which would mean the image has been cut from a larger one and has remembered its original position. To ignore it, repage your image before use... `convert YourImage.png +repage ... -annotate ...` – Mark Setchell Jun 21 '18 at 15:06
  • Yup, that appears to be the case. Beautiful. I would have never figured that out. – StevieD Jun 21 '18 at 15:10
  • docs for Image::Magick don't mention anything about a "repage" option, however. – StevieD Jun 21 '18 at 15:11
  • 1
    I think you can use `$img->Set( page=>'0x0+0+0' );` – Mark Setchell Jun 21 '18 at 15:16
  • Oh, yeah. You are the Magick Man! That did it. You have now saved me probably 6 hours. I owe you many beers. – StevieD Jun 21 '18 at 15:18
  • OK, so now I can do NorthEast for gravity and not have to make any adjustments. It just places the text in the upper right hand corner, no muss, no fuss. Thanks again! – StevieD Jun 21 '18 at 15:21
  • Cool, I may write it up later (after France vs Peru World Cup football match) as an answer that you could maybe accept. – Mark Setchell Jun 21 '18 at 15:36

1 Answers1

1

In general, if you have trouble finding your textual output with ImageMagick, try the following suggestions:

Suggestion 1

Use contrasting fill and stroke so you can be reasonably sure of seeing your text on any background:

magick ... -fill cyan -stroke magenta ...

Suggestion 2

Set gravity to center in case you are writing beyond the edges of your image:

magick ... -gravity center ...

Suggestion 3

Specify the full path to a font file so ImageMagick will find it even if it is not configured into its configuration files:

magick ... -font /Library/Fonts/Impact.ttf ...

Suggestion 4

Repage your image after loading so it forgets any old page settings it may have had when it was maybe previously part of some larger image and was cropped:

magick ... +repage ...

So, in summary, try a command like:

magick -size 100x100 xc:white -gravity center -fill cyan -stroke magenta -font /Library/Fonts/Impact.ttf -annotate 0 "Hi there" result.png

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432