1 #! /usr/bin/perl
2
3 # Joe Gillotti - 10/12/2010 2:00AM
4 # TODO - optimization / concisination
5
6 # Flush it all out immediately (the dots)
7 BEGIN { $| = 1 }
8
9 # Get our shit
10 use strict;
11 use File::Basename;
12 use Digest::MD5 qw(md5 md5_hex md5_base64);
13
14 # Config
15 my $current_folder = '/home/joe/.f/misc_chan/batches/batch01/';
16 my $new_folder = '/home/joe/programming/perl/batch01/';
17
18 # Hash of a file, quickly
19 sub md5_file {
20 open FILE, @_[0] or die "can't open file\n";
21 binmode FILE;
22 my $hash = Digest::MD5->new->addfile(*FILE)->hexdigest;
23 close FILE;
24 return $hash;
25 }
26
27 # Get teh pics
28 my @pics_orig = glob("$current_folder*.{jpg,png,bmp}");
29
30 # Store cool shit here
31 my %pics_existing_hashes;
32 my @pics_uniq;
33
34 print "Generating hashes and basenames..";
35
36 # Gen hashes and basenames
37 foreach (@pics_orig) {
38 my $hash = md5_file($_);
39 unless ($pics_existing_hashes{$hash}) {
40 $pics_existing_hashes{$hash} = 1;
41 push @pics_uniq, basename($_);
42 }
43 print '.';
44 }
45
46 my %pics_new_names;
47 my $name_length = length(scalar @pics_uniq);
48 my $name_iterate = 1;
49
50 print "\nGenerating final names..";
51
52 # Gen new names
53 foreach (@pics_uniq) {
54 $pics_new_names{$_} = '0' x ( $name_length - length("$name_iterate")) . "$name_iterate.jpg";
55 $name_iterate++;
56 print '.';
57 }
58
59 print "\nConverting to smaller jpgs..";
60
61 # Convert all to lovely named compressed jpegs
62 while (my($orig_name, $new_name) = each(%pics_new_names)) {
63 `convert $current_folder$orig_name -quality 90 $new_folder$new_name`;
64 print '.';
65 }
66
67 print "\nDone\n";