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

Did perl can spilt multi-page tiff file to individual pages per file as the below code"i seek before for count page" in case, i would like to use the perl to spilt the tiff file, but it seems no way. would you like to help? thank a lot.

use strict; my @files = glob "D:/pics/tiff/*.tif"; for (@files){ my $images = imagecounter($_); printf "%40s : %3d \n", $_, $images; } sub imagecounter{ my $tiff = shift; my @endian; my $offset = 0; my $index; my $count = 0; open (my $fh, "<", $tiff); binmode $fh; seek ($fh, $offset, 0); read ($fh, $endian[0], 2); $endian[0] = unpack "a2", $endian[0]; if ($endian[0] eq 'II'){ $endian[1] = 'v'; $endian[2] = 'V'; }elsif ($endian[0] eq 'MM'){ $endian[1] = 'n'; $endian[2] = 'N'; }else{ warn "Unknown byte order in $tiff, possible bad file\n"; return 0; } seek ($fh, 4, 0); read ($fh, $offset, 4); $offset = unpack "$endian[2]", $offset; while ($offset){ $count++; my $length; seek ($fh, $offset, 0); read ($fh, $length, 2); $length = unpack "$endian[1]", $length; $index = ($length * 12); seek ($fh, $index, 1); read ($fh, $offset, 4); $offset = unpack "$endian[2]", $offset; if ($count > 100){ warn "Count too high in $tiff, possible bad file\n"; return 0; } } close $fh; return $count; }

Replies are listed 'Best First'.
Re: help;Splitting the TIFF image into individual pages
by zentara (Cardinal) on Feb 19, 2010 at 13:13 UTC
    Googling for "ImageMagick tiff split" yields
    #!/bin/sh convert multipage.tif single%d.tif
    How you would do that with the Perl API is shown on PerlMagic

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: help;Splitting the TIFF image into individual pages
by Anonymous Monk on Feb 19, 2010 at 07:31 UTC
    Use a library, like Imager, ImageMagick...
Re: help;Splitting the TIFF image into individual pages
by hangon (Deacon) on Feb 19, 2010 at 19:04 UTC

    I posted that code here a couple of years ago. It only counts the number of images in a TIFF file, by counting the number of directory entries. To actually split the file, you would need to decode the chain of directory entries to locate the various parts of each image, then extract them and reformat each into the proper TIFF file format. It is certainly doable in Perl, and would also be reasonably fast since decompressing would be unnecessary. However the TIFF spec is a bit complex, so you would probably be better off using existing tools as already mentioned.