Reworked all the hashes (%files, %ar) into hash references ($files, $ar)

for better clarity. Who uses hashes, anyway?

Added some more sanity and error checking.
This commit is contained in:
Uroš Golja
2016-10-25 23:03:39 +02:00
parent f617949281
commit b3f2dd01ba

View File

@@ -9,23 +9,40 @@ use Image::EXIF;
use Data::Dumper::Simple;
use Math::Round;
$Data::Dumper::Indent = 3;
my $conf = {
src_dir => "./pics",
dst_dir => "./links",
};
# verify the source dir
if (! -d $conf->{src_dir}) {
print "ERROR: Source directory '$conf->{src_dir}' does not exist. Aborting.\n" ;
exit 1;
}
# verify the destination dir
if (! -d $conf->{dst_dir}) {
print "ERROR: Source directory '$conf->{dst_dir}' does not exist. Aborting.\n";
exit 1;
}
# gather the list of files
my @list = find_wanted(
sub { -f && ( /\.jpg$/i || /\.jpeg$/i ) },
$conf->{"src_dir"}
);
if (! @list) {
print "No files found.\n";
exit 0
}
# get their information
my (%files, %ars, $i, $errors);
my $count = 0; # how many files?
my $errors = 0; # how many errors?
my $ars = {}; # a hashref that holds all the encountered aspect rations and their number of occurence
my $files = {}; # a hashref that holds all the files, their exif data, and their calculcated aspect ratio
foreach my $f (@list) {
$i++;
$count++;
print "Gathering info for image in file: $f\n";
my $e = Image::EXIF->new($f);
my $exif = $e->get_image_info();
@@ -56,45 +73,45 @@ foreach my $f (@list) {
else {
my $ar;
$ar = nearest(0.01, ($x > $y) ? $x / $y : $y / $x) unless $error_flag;
$ars{$ar}++;
$files{$f}->{"ar"} = $ar;
$ars->{$ar}++;
$files->{$f}->{"ar"} = $ar;
}
# Append the exif data into hash
$files{$f}->{"exif"} = $exif;
$files->{$f}->{"exif"} = $exif;
# bump the number of errors
$errors++ if $error_flag;
}
print "All images and their EXIF data:";
print Dumper %files;
print Dumper $files;
print "\n";
print "Total images found: $i\n";
print "Total images found: $count\n";
print "Images with invalid EXIF data: $errors\n";
print "Aspect ratio information for all images (aspect ratio => number of occurences):\n";
print Dumper %ars;
print Dumper $ars;
print "\n";
# Create destination directories
print "Creating destination directories.\n";
foreach my $d (keys %ars) {
print "Creating destination directories: ";
foreach my $d (keys %$ars) {
my $dirname = $conf->{"dst_dir"} . "/" . $d;
print "Creating directory: $dirname\n";
print "'$dirname' ";
mkdir $dirname;
}
print "\n";
# Symlink
print "Symlinking source files into destination directories.";
foreach my $f (keys %files) {
next unless exists $files{$f}->{"ar"};
print "Symlinking source files into destination directories.\n";
foreach my $f (keys %$files) {
next unless exists $files->{$f}->{"ar"};
# construct the symlink name
my $name = basename($f);
my $ar = $files{$f}->{"ar"};
my $ar = $files->{$f}->{"ar"};
my $dest = $conf->{"dst_dir"} . "/" . $ar . "/" . $name;
# symlink