Listing 12. Append a layer from one image to another.

#!/usr/bin/perl -w

use Gimp ":auto";
use Gimp::Fu;
use lib '/usr/local/bin/';
use MonkeyIQGIMP;

sub monkeyiq_gimp_append_layer_from_image_file {
  my($inputimagename, $outfilename,
    $inputimagenameSecond) = @_;

  print "cat $inputimagename";
  print " $inputimagenameSecond >> $outfilename\n";

  $img  = gimp_file_load( $inputimagename,
                          $inputimagename );
  $img2 = gimp_file_load( $inputimagenameSecond,
                          $inputimagenameSecond );

  $layer = getMergedLayer( $img2 );
  if (!$layer->has_alpha)
  {
    $layer->add_alpha;
  }

  $img2->selection_all;
  $layer->edit_copy;
  $newlayer = Gimp->layer_new(
    $img,
    Gimp->image_width( $img2 ),
    Gimp->image_height( $img2 ),
    RGBA_IMAGE,
    "appended image data",
    100,
    NORMAL_MODE );
  $newlayer->drawable_fill(TRANSPARENT_FILL);
  Gimp->image_add_layer( $img, $newlayer, -1 );
  Gimp->image_lower_layer( $img, $newlayer );
  $floater = $newlayer->edit_paste( 1 );
  $floater->anchor;

  $imgw  = Gimp->image_width( $img );
  $imgh  = Gimp->image_height( $img );
  $img2w = Gimp->image_width( $img2 );
  $img2h = Gimp->image_height( $img2 );

  $img->resize( $imgw >= $img2w ? $imgw : $img2w,
                $imgh >= $img2h ? $imgh : $img2h,
                0, 0 );

  imageOutput( $img, $outfilename );
}

register
   ...
  [
   [PF_STRING, "inputimage",
     "Name of image to load", ""],
   [PF_STRING, "outputimage",
     "Name of image to save to", ""],
   [PF_STRING, "newlayerimage",
     "Name of image to append to inputimage", ""],
  ],
  \&monkeyiq_gimp_append_layer_from_image_file;


if( $#ARGV <= 0 ) {
  print "Usage: $0 -inputimage imagepath";
  print " -outputimage full_dst_path ";
  print " -newlayerimage imagepath2 \n\n";
  exit;
}

# Handle over control to gimp
exit main();

