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

Hi, I'm working on a gis tracking application. I'd like to be able to put a text marker (some ascii character or icon) overtop of a jpg image. So if I have an 800x600 image, I'd like to find a way to take the map and overlap another image at 400x300 (center) for example. Is this something I can do in perl? I know it's great for data, but what about image manipulation?

Replies are listed 'Best First'.
Re: Image Addition
by Zaxo (Archbishop) on Jan 18, 2004 at 06:20 UTC

    Image::Magick, GD, or Gimp modules are likely to help with that. Check their details to see exactly how.

    After Compline,
    Zaxo

Re: Image Addition
by davido (Cardinal) on Jan 18, 2004 at 06:35 UTC
    You might want to have a look at the CPAN modules that fall under the Imager namespace. Imager.pm is the base class, and from there look at Imager::Filters. This module allows you to manipulate images in many ways, including adding "watermarks." Watermarks may be of programmer-definable strength, and can be put at any location in the image.

    Also see, Imager::Font. Together with Imager::Filters, you could probably put a text graphic anywhere you want within another graphic image.

    I hope this helps.


    Dave

Re: Image Addition
by valdez (Monsignor) on Jan 18, 2004 at 11:45 UTC

    This is a little example with Image::Magick:

    #!/usr/bin/perl use Image::Magick; # create an image, white background, and save it $base = Image::Magick->new(); $base->Set(size => '640x480'); $base->ReadImage('xc:orange'); $base->Write('./background.jpg'); # draw some text, crop it and save in a new image $tile = Image::Magick->new(); $tile->Set(size => '600x200'); $tile->ReadImage('xc:white'); $tile->Annotate(font => 'times.ttf', pointsize => 12, fill => 'navy', text => 'www.perlmonks.org', x => 100, y => 100); $tile->Crop('0x0'); $tile->Write('./tile.jpg'); # read background $image = Image::Magick->new(); $image->Read('./background.jpg'); # draw a red pixel $image->Set('pixel[50,50]' => 'red'); # read another image $image2 = Image::Magick->new(); $image2->Read('./tile.jpg'); # compose them $image->Composite(image => $image2, compose => 'Over', x => 150, y => 150); $image->Write('composition.jpg');

    Have fun! Valerio

Re: Image Addition
by xtype (Deacon) on Jan 19, 2004 at 08:15 UTC
    Some people might prefer GD to Image::Magick for its speed and lack of bloat. Also it will install with only one, or all, of the image libraries you choose.
    You could do what you want with GD and the GD::string() function...
    ## extracted and simplified from a bit of working code I have my $fname = "/path/to/your/file.jpg"; my $text = "What you want to say."; open (IMG, "<$fname") or die; my $size = -s $fname; my $data; read IMG, $data, $size; close IMG; die "blah" unless $data; use GD; # my $itype = image_type(getType_bymagic($data)); # die unless $itype eq "Jpeg"; # my $method = "newFrom${itype}Data"; my $method = "newFromJpegData"; my $i = GD::Image->$method($data, 1); my $fg = $i->colorAllocate(102,102,102); $i->string(gdMediumBoldFont,7,7,$text,$fg); return \$i->png;
    ...however, you would then be left with managing the centering and wrapping of text yourself.
    For that reason I would recommend looking into the GD::Text, GD::Text::Align, and GD::Text::Wrap modules.

    Good luck.
    -xtype
Re: Image Addition
by skx (Parson) on Jan 19, 2004 at 10:54 UTC

    Yes it's possible to do this in perl, as the others have already suggested.

    If you didn't know then it might be worth looking at the command line tools from the imagemagick suite.

    For example I wrote a console application to dump out images from my webcam to a file "foo.jpg" and then I call that in a shellscript to add some text to it:

    # Get a temporary file camgrab -device "/dev/video" -output /tmp/image.jpg # Add the date to the bottom of the image. convert -font helvetica -fill black -draw "text 10, 280 '`date`'" /tmp +/image.jpg /tmp/image.jpg

    This isn't exactly what you're looking for as I am adding text to an image, but you can overlay other images too "man convert" will give you the details.

    Steve
    ---
    steve.org.uk