#!/usr/bin/perl -w use strict; # Just a little script to screen the given files for corrupt images # and delete/move/report them if so instructed. # Depends on Image::Magick being installed # -r : delete corrupt images # -q : mention filenames of corrupt images, keep quiet otherwise. # -m directory : move to directory, takes precedence over -r. Will be created # if not already there. # -f file : instead of taking filenames from the command line, take them # from file ("-" if you want to use a pipe) # example : find -type f | cleaner.pl -m ./corrupt -f - # will move all corrupt images to ./corrupt, being pretty verbose use Image::Magick; use Getopt::Std; use File::Basename; getopts('rqm:f:'); our($opt_r, $opt_q, $opt_m, $opt_f, @filelist); my $count=0; if ($opt_f) { open FILELIST, $opt_f; @filelist = ; close FILELIST; } else { @filelist = @ARGV; } if ($opt_m) { if (!-d $opt_m) { mkdir $opt_m or die "can't create directory $opt_m\n"; } } foreach my $file (@filelist) { $count++; chomp $file; if (-f $file) { $opt_q or print "Testing " . sprintf ("%4d",$count) . "/" . sprintf ("%4d", scalar @filelist) . " " . basename($file) . " ... "; my $p = new Image::Magick; my $s = 0; my $error = $p->Read($file); if ($error) { $error =~ /(\d+)/; my $errorcode = $1; if ($errorcode == 330) { $opt_q or print "which does not exist"; } elsif ($errorcode == 325) { $opt_q or print "which seems to be corrupt ... "; $opt_q or (($opt_r or $opt_m) or print "keeping it anyway."); if ($opt_m) { $opt_q or print "moving to $opt_m ... "; ($s = rename $file, $opt_m.'/'.basename($file) and print "done.") or ($opt_q or print "could not move $file to $opt_m"); } elsif ($opt_r) { $opt_q or print "deleting ... "; ($s = unlink $file and print "done.") or ($opt_q or print "could not delete $file"); } # if ($opt_ $opt_q and print "$file\n"; } elsif ($errorcode == 320) { $opt_q or print "which is not an image"; } else { $opt_q or print "which yielded an error : $error"; } # if (errorcode= } else { $opt_q or print "which is fine"; } # if (error) $opt_q or ($s ? print "\n" : print ", skipping.\n"); } # if (-f $file) }