I recently ran into another opportunity to test my perl skills. I wrote a small script that basically changes the compression type for tiff files. At work we have a document control system that creates tiffs. The default settings on the scanners that are used make rather large tiffs for my taste. So instead of adding more disks to the server, I thought I might see if I could reclaim some space. Here is the code I wrote.
As you can see, the script takes a file as it's argument. It then makes a copy of the original and then output the new file. I then have a test to see if the compression actually made the file smaller. There are times when the file will actually get larger. In that case I move the orignal back into place. Using this with the find command makes the job easier. Ex.
find /photo_dir -name '*\.tif' -exec ~/bin/tiffshrink {} \;
This script could also be easily adapted for use with jpegs, etc. If you wanted, you might also write some logic to do certain conversions based on the file extensions.
$ cat tiffshrink
#!/usr/bin/perl
use File::Copy;
use Image::Magick;
$inputfile = @ARGV[0];
copy($inputfile,"$inputfile.orig");
$image = new Image::Magick;
$image->Read("$inputfile.orig");
$image->Write(filename=>$inputfile, compression=>'Zip', monochrome=>'True');
$inputsize = -s "$inputfile.orig";
$outputsize = -s "$inputfile";
copy("$inputfile.orig",$inputfile) if ($inputsize < $outputsize);
unlink("$inputfile.orig");
As you can see, the script takes a file as it's argument. It then makes a copy of the original and then output the new file. I then have a test to see if the compression actually made the file smaller. There are times when the file will actually get larger. In that case I move the orignal back into place. Using this with the find command makes the job easier. Ex.
find /photo_dir -name '*\.tif' -exec ~/bin/tiffshrink {} \;
This script could also be easily adapted for use with jpegs, etc. If you wanted, you might also write some logic to do certain conversions based on the file extensions.
Comments
Post a Comment