0

I recently took over someone else's position on a project. When the below code was run on their old machine, it worked fine. But when I run it, I get a blank white line with a yellow rectangle with a red outline. Essentially, the Draw() method works fine, but the Annotate() method isn't doing anything. No matter what values I pass it for text, font, fontsize, or color, no text will show up.

Here's the relevant code:

my ($length,$width,$boxRef,$text,$font,$fontSize,$fontColor,$outFile) = @_;

my $image = Image::Magick->new;
$image->Set(size=>$length."x".$width);
$image->ReadImage('xc:white');
$image->Draw(primitive=>'rectangle',stroke=>'red',fill=>'yellow',points=>"$$boxRef[0],$$boxRef[1],$$boxRef[2],$$boxRef[3]");
my $y = $image->Annotate(text=>$text,font=>$font,fill=>$fontColor,pointsize=>$fontSize,geometry=>'+0+20');
warn "$y" if "$y";
my $x = $image->Write($outFile);
warn "$x" if "$x";
undef $image;

I'm pretty sure nothing is wrong with the code, as other simpler scripts won't work either. I believe there is something wrong with my machine or installation of ImageMagick, but I have no idea what is wrong.

My boss said that he remembers the previous person struggling with some font issue, so maybe that has something to do with it? I've been doing some digging the past day or so, but I haven't found anything that seems to be quite like my problem that offers a good solution.

Does anyone know why I can't get ImageMagick to display text?

Some information about my setup:

OS X Mavericks 10.9.3; ImageMagick 6.8.9-1 / PerlMagick 6.88 (installed thru Homebrew)

BrianAtUAB
  • 23
  • 3
  • Try `brew update; brew upgrade; brew upgrade $(brew outdated); brew doctor` – Mark Setchell Jun 06 '14 at 19:32
  • Already did. In fact, I reinstalled Homebrew earlier today to deal with another problem. Not that it seems to matter; I had the same error before and after the reinstall. – BrianAtUAB Jun 06 '14 at 19:35
  • If you care to make a little self-contained Perl program with some sensible numbers all hard-coded in, I can try it on my Mavericks installation for you... – Mark Setchell Jun 06 '14 at 19:44
  • Alright, here's one that is really similar to mine: `#!/usr/bin/perl use Image::Magick; use strict; use warnings; my $image = new Image::Magick; $image->Set( size => '500x500' ); $image->ReadImage( 'xc:white' ); $image->Annotate( text=>'This is a cool line of text', font=>'Arial', fill=>'black', pointsize=>'12', geometry=>'+0+20' ); $image->Write( 'TextOut.png' );` Sorry for the blob, I don't know how to format comments :/ – BrianAtUAB Jun 06 '14 at 19:57
  • I just got this off of a blog btw, I don't know how well it works. B/c of my problem, I can't test it myself @MarkSetchell – BrianAtUAB Jun 06 '14 at 20:02
  • Mmmm... that just gives me a big 500x500 blank white image... OSX 10.9.3. Image Magick Version: 6.8.9-1 Q16 x86_64 2014-05-12 – Mark Setchell Jun 06 '14 at 22:00

1 Answers1

0

I figured it out everyone! It turns out it was indeed an issue with the fonts. I found the solution on a perlmonks post. Even if the font used is on the machine, sometimes it will not find it (but on other machines it will for whatever reason). So the location has to be hard-coded in.

Changed the annotation line to the following:

# font has to be hard-coded in
my $y = $image->Annotate(text=>$text,font=>'/System/Library/Fonts/Courier.dfont',fill=>$fontColor,pointsize=>$fontSize,geometry=>'+0+20');

Hope this helps anyone else who comes across this problem!

BrianAtUAB
  • 23
  • 3