Compare images in a folder, find identical
Post ReplyCompare images in a folder, find identicalPosted: Friday, October 25, 2013 [20:50:24] - 1
When processing bulk images or collecting them - often there are tons of duplicates. Weeding them out manually could be an impossible task. Here is a quick Perl/ImageMagick/NetPBM hack: $imgdir = $fromdir; $rescmp = $svres.'/test.comp.gif'; ## FOR WHATEVER REASON ImageMagick wouldn't process without saved-file unless(-f "$svres/images.sizes.txt") { ################## COMPILE TXT FILE IF NEEDED ####### @all = glob("$imgdir/*"); ## GET PHYSICAL FILE SIZES FIRST foreach $f (@all) { if($f =~ m/\/sm\./) {next;} $size = (stat ("$f"))[7]; $sifiles{$size}{$f}=$origsize; } ## NOW SAVE THE DATA IN A TEMP FILE WITH FILE SIZE AND IMAGE SIZE open(TXT,">$svres/images.sizes.txt"); foreach $size (sort keys(%sifiles)) { print "$size\:\n"; } ## END FOREACH close(TXT); } ######################################################### COMPILE TXT FILE IF NEEDED ##### unless(-f "$svres/im.exacts.txt") { ################################# COMPILE EXACTS FILE ################ $d = `cat $svres/images.sizes.txt`; foreach $l (@all) { ($s,$f,$os,$wh) = split(/\t/,$l); unless(-f $f) {next;} $check{$s}{$wh}{$f}=1; ## HASH BY: FILE SIZE/IMAGE SIZE } ## FOREACH LINE END foreach $s (sort num keys(%check)) { ## FILE SIZE } ## FOREACH SIZE END %check=(); print "\n\n"; foreach $s (sort num keys (%docompare)) { } ## FOREACH SIZE END %docompare=(); ## SAVE RESULTS IN A FILE open(EXC,">$svres/im.exacts.txt"); foreach $f (sort keys (%exacts)) { @more = keys %{$exacts{$f}}; if($nmore < 1) {next;} print EXC "$f"; print EXC "\n"; } ## FOREACH 1 END close(EXC); } ################################################################### COMPILE EXACTS FILE ################ ## ACTUALLY COMPARE IMAGES sub comparethem { ##### $from = shift(@cmpr); } ## END COMPARE ###### sub getlowest { ## GET THE FILE SIZES WITH ZERO ON THE END i.e. file size 17684 converted to 17680 } ## END GET LOWEST sub pnmsize { ## GET THE IMAGE SIZE IN PIXELS my($pnm) = @_; ($width,$height)=$pnm=~m#^P\d+\s+(\d+)\s+(\d+)\s+\d+\s#ois; unless($width && $height) {($width,$height)=$pnm=~m#^P\d{1,4}\s+(\d+)\s{1,3}(\d+)\s#ois;} } ## END SUB PNM SIZE ## sub num {$a <=> $b;} ## SORT THINGS Script runs fine on folders with 100,000 images. On the end you'll get im.exacts.txt file with exact matches. This is not perfect, but it works. |
RE: Compare images in a folder, find identicalPosted: Friday, October 25, 2013 [20:56:47] - 2
To read the output visually on Mac: unless(-f $fromfile) {print "$fromfile not present\n"; $d = `cat $fromfile`; foreach $l (@all) { @lz = split(/\t/,$l); unless($origfl) {next;} `open $origfl`; print "Original file: $origfl\t$orfname\n"; print "See more? \[y\]: "; $forw = <STDIN>; if($forw) {exit(0);} } ## FOREACH LINE END It uses "open" which I am not sure present on other UNIX flavors. |