http://www.perlmonks.org?node_id=491827

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

I'd like to be able to perform the same action as the following convert.exe command line in Perl

convert.exe -pointsize 200 -stroke #ff0000 -fill #808040 -draw "text 8 +00,650 'Hello world'" -identify "x.JPG" xb.jpg

The following code runs without warnings and does generate xb.jpg, but without the 'Hello world' text:

use strict; use warnings; use Image::Magick; my $image = Image::Magick->new (); $image->ReadImage( 'x.JPG' ); my $err = $image->Draw ( pointsize=>200, stroke=>"#ff0000", fill=>"#90ff40", x=>700, y=>650, primitive=>"text 'hello world'" ); print $err if $err; $image->Write( 'xb.jpg' );

What I would really like to be able to do is generate the text with about 50% transperancy, but just generating the text in the output image would be a very good start.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re: Image Magick convert as Perl
by davidrw (Prior) on Sep 14, 2005 at 12:53 UTC
    You're looking for the Annotate() method instead of Draw ...
    my $err = $image->Annotate( pointsize => 200, text => 'hello world', x => 700, y => 650, stroke => '#ff0000', fill => '#90ff40', font => '@ARIAL.TTF', # for ttf, it's at-sign then the path to the +.ttf file .. # grabbing these .ttf's from a windows box is + fine );

      Yes, I was aware of Annotate, but I'd used draw in the command line version and there it worked. Annotate will suffice, but I'm still puzzled that the Draw doesn't work. The documentation does not seem to describe a way of supplying the text to be drawn for Draw and supplying the text as (... text=>"Hello World", ...) generates an error to the effect that "text" is not a valid parameter (I've not got the code here).

      My other question is: how do I generate 50% transparent text? Have I to do it by generating the text in another image then compose the two images together, or is there a more direct way of generating partially transparent text on an image?

      An even more fundamental question is: How do I generate a "blank" image that I can draw on?


      Perl is Huffman encoded by design.
        An even more fundamental question is: How do I generate a "blank" image that I can draw on?
        I'll take that one first (reference: perl-magick -- search for "xc" in the Examples section):
        my $image = Image::Magick->new(); $image->Set(size=> "800x600"); $image->ReadImage('xc:white');
        See also this formats page for what IM takes (XC is listed there as well as some built-in images and patterns).

        Annotate will suffice, but I'm still puzzled that the Draw doesn't work.
        me too.. i guess it's just another IM inconsistancy .. (note at least in the http://imagemagick.org/script/perl-magick.php|docs] that Draw doesn't list a 'text' parameter)

        My other question is: how do I generate 50% transparent text?
        umm.. no clue.. But i suspect if there is an answer it might be on this site: ImageMagick v6 Examples -- seems like a very good collection of faq/how-to's...