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

I am trying to merge 2 tiff images (check images - front and back) into a single tiff file using Imager.pm. Merged image file size is slightly greater than the sum of the images but its resulting into a single image (either the front or back image depending on the order of merge). Can someone please help in resolving this issue?

Code snippet I am using:

#! /usr/bin/perl use Imager; my @im; my $tiffile = "./merged_image.tif"; unlink $tiffile; my @images = glob("./*.tif"); print @images, "\n"; my $i=0; for my $file (@images) { my $im = Imager->new(); $im->read(file=>$file) or die "$0: Cannot read image $file: ",$im->e +rrstr,"\n"; my @alltags = $im->tags; foreach $tag ( @alltags ) { print $tag->[0], ":", $tag->[1], "\n"; } push(@im, $im); $i = $i + 1; } ## writing the above images to a single file Imager->write_multi({ type=>'tiff', file=>$tiffile, }, @im) or die "$0: cannot save to $output: ",$Imager::ERRSTR,"\n"; #checking merged image properties my $im = Imager->new(); $im->read(file=>$tiffile) or die "$0: Cannot read image $file: ",$im-> +errstr,"\n"; my @alltags = $im->tags; foreach $tag ( @alltags ) { print $tag->[0], ":", $tag->[1], "\n"; } Output: haikz@eidspstd99:/usr/home/shaikz ==> ./test2.pl ./009600209943_2.tif./009600209943_3.tif tiff_bitspersample:1 tiff_photometric:0 tiff_resolutionunit:2 tiff_resolutionunit_name:inch i_xres:200 i_yres:200 i_format:tiff tiff_compression:fax4 tiff_bitspersample:1 tiff_photometric:0 tiff_resolutionunit:2 tiff_resolutionunit_name:inch i_xres:200 i_yres:200 i_format:tiff tiff_compression:fax4 tiff_bitspersample:1 tiff_photometric:0 tiff_resolutionunit:2 tiff_resolutionunit_name:inch i_xres:200 i_yres:200 i_format:tiff tiff_compression:fax4

Replies are listed 'Best First'.
Re: Merging 2 tiff images into a single image
by jethro (Monsignor) on Jul 05, 2012 at 17:33 UTC

    Are you sure that the program you are checking the file with can display/work with multi-image tiff-files at all? Tiff seems to allow multi-image files, but that doesn't mean that a picture-viewer necessarily can handle that

    Also a tiff multi-image seems to mean that there are just multiple images stored in this file, it doesn't necessarily mean 'merged'. What do you mean by merged? Should the images be added as a superimposed picture, should the two pictures be appended horizontally or vertically? Or should they really stay separated (which I wouldn't really call merged)?

    If for example you want to create one big picture made out of the two pictures fitted together horizontally, you probably have to create an image of the appropriate size and then paste the source images into this image. This might be possible with the 'paste' method of Imager::Transformations

      Thanks for looking into this. I am using MS Office Picture Manager. I want the 2 images to be next to each other. I have picked the wrong word to describe the situation. Thanks for pointing that. I am trying with paste functionality but not there yet. Will keeep posted.

Re: Merging 2 tiff images into a single image
by zentara (Cardinal) on Jul 05, 2012 at 17:42 UTC
    but its resulting into a single image (either the front or back image depending on the order of merge).

    That is because write_multi is meant for making animated images, like an animated tiff or animated gif. The first image is the one that is first in the animation stack.

    What you want is the paste method. I have an example that shows what to do at Mirrored text with Imager. It pastes a mirror image side by side, but with a small amount of manipulation, you could make the code paste your 2 images top and bottom. You could resize your pasted images afterwards if desired. Read perldoc Imager::Transformations for more information and code.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thanks for pointing out the functionality of write_multi. I looked at your sample and it is definitely helpful. Working with paste function but not there yer. Will keep posted.