Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Overlaying text in existing image (jpg) file

by silent11 (Vicar)
on Oct 19, 2004 at 19:08 UTC ( #400636=perlquestion: print w/replies, xml ) Need Help??
silent11 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a perl script to write text in an image file. Basically the image will serve as a background, I want to overlay some text over the background image, saving a different copy of the first image. that's it. I looked around the GD docs and couldn't find anything realted.

I seem to remember a post here where a monk took random feedback from ebay and inserted it over random images from yahoo. For the life of me I can't find that post :(

Any ideas or suggestions would be greatly appreciated.

Spread Firefox
  • Comment on Overlaying text in existing image (jpg) file

Replies are listed 'Best First'.
•Re: Overlaying text in existing image (jpg) file
by merlyn (Sage) on Oct 19, 2004 at 19:32 UTC
      The documentation is found here
        The documentation is found here
        Heh. That's sort of a sketch of the docs. That's not the real docs. The real docs do not exist. Most of the time, I end up starting from one of the tests in the Perl module distro, and then slowly mutating it to make sure it still works and is still doing what I want it to do.

        On a recent 15-line script (to grab the the timestamps from the EXIF data), I ended up having to stare at the C sources for about an hour, and just "try" things repeatedly. I'm no dummy, and it took me that long. Gah.

        I'm not sure what it is about image-manipulation software that makes them all: (a) fragile, (b) underdocumented, and (c) hard to install. It's very consistent.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Overlaying text in existing image (jpg) file
by Juerd (Abbot) on Oct 19, 2004 at 20:35 UTC

    Several modules make this possible. PerlMagick, GD and Imager are some of them. Imager has my preference.

    Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

Re: Overlaying text in existing image (jpg) file
by zentara (Archbishop) on Oct 20, 2004 at 14:31 UTC
    Here is a simple example ImageMagick
    #!/usr/bin/perl use Image::Magick; #print "Content-type: image/jpg\n\n"; my $jpeg_file = 'frontpage.jpg'; my $im = Image::Magick->new(); my $rc = $im->Read($jpeg_file); die $rc if $rc; $rc = $im->Annotate( gravity => "Center", text => "Hello ImageMagick!", font => "arial.ttf", stroke => "red", fill=> "red", pointsize => 28, ); die $rc if $rc; $rc = $im->Write("$0.jpg"); die $rc if $rc;

    And here is one for GD

    #!/usr/bin/perl use warnings; use strict; use GD; use GD::Text::Align; @ARGV or die "Need an image file\n"; for my $img_file (@ARGV) { my $gd = GD::Image->new($img_file) or die; my ($w, $h) = $gd->getBounds(); my $gdt = GD::Text::Align->new($gd, valign => 'bottom', halign => 'right', text => 'Some Text', colour => $gd->colorResolve(0,0,0), ) or die; $gdt->set_font('arial', $h/10) or die; $gdt->draw($w, $h, 0) or die; open(GD, ">$0.png") or die; binmode GD; print GD $gd->png; close GD; }

    I'm not really a human, but I play one on earth. flash japh
Re: Overlaying text in existing image (jpg) file
by ikegami (Pope) on Oct 19, 2004 at 19:32 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://400636]
Approved by FoxtrotUniform
[Corion]: Test...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-06-23 16:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.