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

I'd like to overlap two .gifs, for instance I'd like to be able to dynamically make a collage of photos. any ideas?

Replies are listed 'Best First'.
Re: merging two .gif or jpg (overlapping)
by liverpole (Monsignor) on Jan 07, 2006 at 00:25 UTC
    Yes, ImageMagick is probably a good idea for this.  I don't know offhand whether it natively supports merging two images, but you could convert the images to XPM, and then perform your own image manipulation on the lines from the XPMs.

    Here's an example (from some code I recently wrote) of a subroutine for doing this called convert_image_to_xpm() :

    #!/usr/bin/perl -w # # A subroutine for reading jpg and gif images, and converting # them to XPM format # # 060106 liverpole # ############## ### Strict ### ############## use strict; use warnings; ################# ### Libraries ### ################# use File::Basename; use FileHandle; use Image::Magick; ################### ### Subroutines ### ################### sub convert_image_to_xpm($) { my ($fname) = @_; print "=== Converting file $fname to XPM format ===\n"; my $image = new Image::Magick(); my $result = $image->Read($fname); $result and die "Error reading image file '$fname' ($result)\n"; print "Read image '$fname', now writing ...\n"; my $tmp = "convert.$$.xpm"; $result = $image->Write($tmp); $result and die "Error writing .xpm file '$tmp' ($result)\n"; my $fh = new FileHandle(); open($fh, "<$tmp") or die "Cannot read file '$fname' ($!)\n"; chomp (my @lines = <$fh>); close $fh; print "Converted image to .xpm format\n"; unlink $tmp; return \@lines; } #################### ### Main program ### #################### my $iam = basename $0; (my $img = shift) or die " syntax: $iam <image file> Demonstrates usage of the subroutine convert_image_to_xpm, for converting a given .jpg or .gif <image file> to XPM format. "; my $plines = convert_image_to_xpm($img); foreach my $line (@$plines) { print "$line\n"; }
    Once it's converted to XPM format, it's very easy to manipulate the image any way you want to.

    One other note -- if you're a fan of gvim, when you edit an XPM image, it does something pretty cool:  it renders the actual colors for each pixel in the image.  (Don't try it for images which are too large, though).  This makes it very easy to see how the XPM will display, as well as edit it at the same time.


    @ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"
Re: merging two .gif or jpg (overlapping)
by mrborisguy (Hermit) on Jan 06, 2006 at 23:23 UTC
Re: merging two .gif or jpg (overlapping)
by renodino (Curate) on Jan 07, 2006 at 00:44 UTC
    You fail to mention your target medium for display. If you truely want to merge a bunch of images into 1 big PNG/GIF/etc., then GD may be a (simple) way to go. While it doesn't have any fancy GUI tool for pasting images into the result, its pretty small footprint, and if you know how you want to layout things, its pretty easy to just load & paste images.

    OTOH, if you're really looking to layer some images together into a web page, you may want to google for some Javascripts.

Re: merging two .gif or jpg (overlapping)
by BrowserUk (Patriarch) on Jan 07, 2006 at 00:43 UTC

    When you say "merge", do you mean making one image from two where the 'upper' image completely covers the lower image (like you had physically pasted one photo atop another); or are you looking to merge their alpha channels (as demo'd here) ?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: merging two .gif or jpg (overlapping)
by saintmike (Vicar) on Jan 07, 2006 at 05:13 UTC
    Not 100% sure what you mean by 'overlap' but if it's either morphing two images or making a 'montage', here's a couple of tricks using the CPAN module Image::Magick: The article is in German, but the perl code and the resulting images should be self-explanatory.