Compare commits

1 Commits

Author SHA1 Message Date
Uroš Golja
a84c0d7e55 Ignored some more stuff. 2017-01-05 20:22:16 +01:00

View File

@@ -7,7 +7,7 @@ use File::Find::Wanted;
use File::Basename;
use File::Spec;
use File::Spec::Functions;
use Image::Size;
use Image::EXIF;
use Data::Dumper::Simple;
use Math::Round;
@@ -59,42 +59,64 @@ if (! @list) {
exit 0
}
# Go through all the image files and build the hash that holds all the
# information about them.
# extract the EXIF data from the files and build a hash from all that stuff
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) {
$count++;
msg "Gathering sizes for image in file: '$f'\n";
(my $x, my $y) = imgsize($f);
# calculate aspect ratio
my $ar;
if ($x and $y) {
$ar = nearest(0.1, ($x > $y) ? $x / $y : $y / $x);
$ars->{$ar}++;
msg "Gathering info for image in file: '$f'\n";
my $e = Image::EXIF->new($f);
my $exif = $e->get_image_info();
my ($x, $y, $error_flag);
# Check for the existence of x
if (!defined $exif->{"Image Width"}) {
msg "Could not find attribute 'Image Width' for image '$f'.\n";
$error_flag = 1;
}
else {
$errors++;
$x = $exif->{"Image Width"};
}
# append the stuff to hash
$files->{$f}->{x} = $x;
$files->{$f}->{y} = $y;
# Check for the existence of y
if (!defined $exif->{"Image Height"}) {
msg "Could not find attribute 'Image Height' for image '$f'.\n";
$error_flag = 1;
}
else {
$y = $exif->{"Image Height"};
}
# Calculate aspect ratio and append it into the hash
my $ar;
if ($error_flag) {
msg "Unable to calculate aspect ratio for image '$f'.\n";
$ar = "unknown";
}
else {
$ar = nearest(0.01, ($x > $y) ? $x / $y : $y / $x) unless $error_flag;
}
$ars->{$ar}++;
$files->{$f}->{ar} = $ar;
# Append the exif data into hash
$files->{$f}->{exif} = $exif;
# bump the number of errors
$errors++ if $error_flag;
}
# Dump the hash data if we are in debug mode.
# Dump the EXIF data of all images
if ($conf->{debug}) {
msg_debug "All images and their data:\n";
msg_debug "All images and their EXIF data:\n";
print Dumper $files;
msg_debug "\n";
}
# Dump some statistics.
msg "Number of images found: $count. Number of images with errors: $errors.\n";
# Dump some statistics
msg "Total images found: $count. Images with invalid EXIF data: $errors\n";
msg "Aspect ratio information for all images (aspect ratio => number of occurences):\n";
print Dumper $ars;
@@ -113,8 +135,8 @@ msg "Symlinking source files into destination directories.\n";
foreach my $f (keys %$files) {
next unless exists $files->{$f}->{ar};
# Construct the path of the symlink. This is quite tricky to get right.
# TODO: use something smarter than just string concatenation.
# Construct the symlink path. TODO: use something smarter than just string
# concatenation.
msg_debug "\$f='$f' ";
my $ar = $files->{$f}->{ar}; msg_debug "\$ar='$ar' ";
my $symlink_dir = $conf->{dst_dir} . "/" . $ar; msg_debug "\$symlink_dir='$symlink_dir' ";
@@ -122,11 +144,10 @@ foreach my $f (keys %$files) {
my $symlink_dst = File::Spec->abs2rel(
File::Spec->rel2abs($f),
File::Spec->rel2abs($symlink_dir)
);
msg_debug "\$symlink_dst='$symlink_dst' ";
); msg_debug "\$symlink_dst='$symlink_dst' ";
msg_debug "\n";
# Create the symlink.
# symlink
msg_debug "symlink($symlink_dst, $symlink);\n";
symlink($symlink_dst, $symlink);
}