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

Hi, everyone. Very long time, no post. Unfortunately I have not had much time for Perl hackery of late. :(

I am attempting to use Image::Magick to convert some mighty TIFFs (about 160MB each) into some more manageable scaled-down PNGs. Here is my code:

#!/usr/bin/perl use strict; use warnings; use Image::Magick; my $dir = shift @ARGV; # target directory, first argument. for(@ARGV) { # loop over file list print "processing file $_\n"; unless ( /\.tif?$/ ) { warn "$_ does not have a proper TIFF extension...skipping."; next; } my $image = Image::Magick->new; my $err; print "loading $_...\n"; $err = $image->read($_); warn $err if $err; print "scaling $_...\n"; $err = $image->Scale( geometry => '850x636' ); warn $err if $err; s{/.*/}{}; m/(.*?)\./; my $name = $dir . '/' . $1 . '.png'; print "writing $name...\n"; $err = $image->write( $name ); warn $err if $err; undef $image; }

The script is supposed to simply run through each TIFF file on the command line, scale it down, and output it as a PNG in the target directory. For reasons I can't figure out, it seems that the $image->write() method is writing two PNG files for each image. For example, img_0001.tif should produce img_0001.png. But instead, I get img_0001.png.0 and img_0001.png.1. The "0" images appear fine, and the "1" images are somewhat smaller (bytewise) and appear to have much higher compression. Here is my script's output and an example of the target directory after running the script on a CD with four images on it.

[friedo@localhost friedo]$ perl imgprep.pl /home/friedo/imgs/P-2004-00 +1/ /mnt/cdrom/* processing file /mnt/cdrom/img_0001.tif loading /mnt/cdrom/img_0001.tif... Exception 315: incorrect count for field "DateTime" (18, expecting 20) +; tag ignored. (/mnt/cdrom/img_0001.tif) at imgprep.pl line 22. scaling /mnt/cdrom/img_0001.tif... writing /home/friedo/imgs/P-2004-001//img_0001.png... processing file /mnt/cdrom/img_0002.tif loading /mnt/cdrom/img_0002.tif... Exception 315: incorrect count for field "DateTime" (18, expecting 20) +; tag ignored. (/mnt/cdrom/img_0002.tif) at imgprep.pl line 22. scaling /mnt/cdrom/img_0002.tif... writing /home/friedo/imgs/P-2004-001//img_0002.png... processing file /mnt/cdrom/img_0003.tif loading /mnt/cdrom/img_0003.tif... Exception 315: incorrect count for field "DateTime" (18, expecting 20) +; tag ignored. (/mnt/cdrom/img_0003.tif) at imgprep.pl line 22. scaling /mnt/cdrom/img_0003.tif... writing /home/friedo/imgs/P-2004-001//img_0003.png... processing file /mnt/cdrom/img_0004.tif loading /mnt/cdrom/img_0004.tif... Exception 315: incorrect count for field "DateTime" (18, expecting 20) +; tag ignored. (/mnt/cdrom/img_0004.tif) at imgprep.pl line 22. scaling /mnt/cdrom/img_0004.tif... writing /home/friedo/imgs/P-2004-001//img_0004.png... processing file /mnt/cdrom/scan log.txt file /mnt/cdrom/scan log.txt does not have a proper TIFF extension...s +kipping. at imgprep.pl line 14. [friedo@localhost P-2004-001]$ ls -al total 3564 drwxrwxr-x 2 friedo friedo 4096 Nov 11 03:22 . drwxrwxr-x 3 friedo friedo 4096 Nov 11 02:23 .. -rw-rw-r-- 1 friedo friedo 391734 Nov 11 03:17 img_0001.png.0 -rw-rw-r-- 1 friedo friedo 310422 Nov 11 03:17 img_0001.png.1 -rw-rw-r-- 1 friedo friedo 673542 Nov 11 03:19 img_0002.png.0 -rw-rw-r-- 1 friedo friedo 412338 Nov 11 03:19 img_0002.png.1 -rw-rw-r-- 1 friedo friedo 560091 Nov 11 03:21 img_0003.png.0 -rw-rw-r-- 1 friedo friedo 373808 Nov 11 03:21 img_0003.png.1 -rw-rw-r-- 1 friedo friedo 515991 Nov 11 03:22 img_0004.png.0 -rw-rw-r-- 1 friedo friedo 357368 Nov 11 03:22 img_0004.png.1

I appreciate any help. I'm sure it's something really stupid, but it's 3:30 in the morning and I'm stumped.

Thanks.

20041116 Edit by ysth: readmore tags

Replies are listed 'Best First'.
Re: Image::Magick writing more files than I want.
by Corion (Patriarch) on Nov 11, 2004 at 08:47 UTC

    My guess is that your TIFF files contain more than one image - TIFF files can be that way. For example, the smaller, uglier image might be the thumbnail or preview stored with the image file.ImageMagick gives you a way to access the single images in a sequence:

    # from memory, untested: my $img = Image::Magick->new(); my $res = $img->Read( filename => $filename ); die $res if $res; my @images = @$img; printf "Found %s image(s)\n", scalar @images; for my $output_image (@images) { print "Writing $target"; my $target = sprintf "test.%s.png"; $output_image->Write( filename => $target ); print "\n"; };
Re: Image::Magick writing more files than I want.
by chb (Deacon) on Nov 11, 2004 at 09:14 UTC
    Is it possible that the TIFFs all have a (small, high compression) preview image embedded? Maybe that ends up as *.png.1. IIRC one TIFF-file can hold more than one image. Update: yay, Corion was faster....
      You are both correct. The TIFFs contain a preview image that I completely forgot about! Image::Magick was actually scaling those tiny images UP, which explained the awful resolution. Thanks for the help.