coldfingertips has asked for the wisdom of the Perl Monks concerning the following question:

I just installed it today and am having problems getting something to work. Can someone tell me why the Annotate isn't writing "Hi there" on top of the image like I expect it to?

My other question is, is it possible to automatically open the image in imDisplay? How would you do this? I tried  imDisplay "test.png" but it errors out with countless problems.

#!/usr/bin/perl use warnings; use strict; use Image::Magick; my ($image, $x); $image=Image::Magick->new; $x = $image->Read('newbanner.jpg'); warn "$x" if "$x"; my $text = "Hi there"; $image->Annotate(font=>'kai.ttf', pointsize=>40, fill=>'green', text=> +$text); $x = $image->Write('test.png'); warn "$x" if "$x"; print "The file $x has been created";

Replies are listed 'Best First'.
Re: Image::Magick problems
by PodMaster (Abbot) on Jan 19, 2004 at 06:53 UTC
    You need to do error checking at every step (how else can you diagnose when something goes wrong).

    You should get (with code you have now, I did) a Warning 315: Unable to read font (kai.ttf) [No such file or directory] ...

    You need to specify a full path to the font file (at least I do $ENV{WINDIR}.'\\Fonts\\Flubber.ttf',).

    You need to specify where to draw the text (x,y).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Okay, I tried using your code and and I still have an unchanged image. I added an or die "", it catches!! That's a good thing, but it comes back empty. It prints "Something has happened:" at line 16.

      $image->Annotate(font=>$ENV{WINDIR}.'\\Fonts\\arial.ttf', pointsize=>40, fill=>'green', text=>$text, x=>'40', y=>'40') or die "Something happened: $!"; I KNOW I have arial.tff, I checked it myself. Any other suggestions?

        ImageMagick doesn't store error messages in $! (but you knew that).
        #!/usr/bin/perl use warnings; use strict; use strict; use warnings; use Image::Magick; my $label = Image::Magick->new( size => "600x600", ); $label->Read("xc:white"); $label->Draw( primitive => 'line', points => "300,100 300,500", stroke => '#600', ); $label->Draw( primitive => 'line', points => "100,300 500,300", stroke => '#600', ); $label->Draw( primitive => 'rectangle', points => "100,100 500,500", fill => 'none', stroke => '#600', ); my $x = $label->Annotate( text => "North West", # 'x' => 150, # 'y' => 150, pointsize => 40, fill => 'green', # font => 'Flubber.ttf', font => $ENV{WINDIR}.'\\Fonts\\Flubber.ttf', ); warn "$x" if "$x"; $x = $label->Blur(sigma =>1); warn "$x" if "$x"; $x = $label->Write(__FILE__.".png"); warn "$x" if "$x"; __END__
        update: Remember me saying how you need to specify x,y? See how it's comment it out? Think about it for a second.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Image::Magick problems
by Roger (Parson) on Jan 19, 2004 at 05:40 UTC
    Can someone tell me why the Annotate isn't writing "Hi there" on top of the image like I expect it to...

    Could you describe what Annotate did on your image? And what did you expect the output to be? What is the dimension of 'newbanner.jpg'?

    imDisplay ... errors out with countless problems

    What sort of errors are you getting anyway?

      Annotate changed the image in no way, the only change was the image extension into a new .png file. The size of the test image is 500x75.

      My attempt at imDisplay "test.png"; resulted in "Syntax error at line 25 near "imDisplay = "test.png";, Execution of..aborted..

        XX XX XX XXXXXXX ^ ^
        The origin of the TTF font character is left bottom, your drawing routine assumes Y=0, which means the font is drawn off the image. Give it a positive Y value, say 40.

        Also you need to specify the fullpath to the TTF font. Annotate will not draw the text if the font file does not exist. Do you have Chinese fonts installed on your system? That 'kai.ttf' is a Chinese font which I think might not exist on your computer. ;-)

        If you want to display the image with imDisplay, add the following to the end of your script:
        system("imDisplay test.png");

Re: Image::Magick problems
by theAcolyte (Pilgrim) on Jan 19, 2004 at 09:20 UTC
    In addition to having the full path to your font, you must also have ghostscript installed on your system and have ghostscript in your path.

    If you don't have gs, then even with the full path of the font you're going to get no result.

    Just a heads up for when you get that far =)

    - Erik

Re: Image::Magick problems
by Koosemose (Pilgrim) on Jan 20, 2004 at 00:18 UTC
    The presence or absence of ghostscript shouldn't matter, I have annotated with no problem, and don't have ghost script .

    probably the simplest way to put your image in a certain spot on the image is to use gravity, for example, if you want to place the image in the upper right hand corner you would use something along the lines of this:
    $image->Annotate(y=>+14,x=>+4, gravity=>'NorthEast', font=>$font, poin +tsize=>12, fill=>'green',text=>$text);
    The options on gravity are, any of the basic directions on the compass (i.e. North, East, NorthEast, etc...) and Center.

    Also a simple way to pop the image up on IMDisplay is: $image->Write('win:');
Re: Image::Magick problems
by chanio (Priest) on Jan 20, 2004 at 02:08 UTC
    I have been having problems with IM in some (originally *NIX) perl scripts. The scripts where not detecting the Image::Magick.pm. So I have found this page that explains some recipies to solve any win32 problem related to a bad compilation of the win32 version...

    I have added the recipie at Code Catacombs/Win32 Stuff.

    In short, it all comes from not installing the IM application in the C:\\imagemagick\ directory. And not copying the CORE*.dll files in C:\perl\bin\.

    My problems were solved after following this recipie.

      On windows platform you could just download and install the binary release package from the IM website. Although it was compiled for version 5.8.0 of ActiveState perl, I tested it under version 5.8.2 and it appeared to work fine.

        Sorry,Roger

        Not for me.

        I don't know much of the reason why the most recent version of www.fuzzymonkey.org Mike's Photo Gallery Script was not working as the previous at my local Win XP pro. I tryied everything to get the script see the IM module, but it didn't. Finally, after installing IM twice from IM's site (with PerlMagick) and later, following that recipie, it all worked fine.

        It would be great to invest some time in checking all the script to find out why it was working that way. But it is not the first time that I try to use a script that depends on IM at Win32 and it doesn't work.

        It is a shame because IM represents a lot in LINUX, but it can't be used the same at Windows.