diff --git a/.gitignore b/.gitignore index d90722d..a2903be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ links/* pics/* slike/* +linki/* diff --git a/symlink-images-by-aspect-ratio.pl b/symlink-images-by-aspect-ratio.pl index ed27184..5b82e81 100755 --- a/symlink-images-by-aspect-ratio.pl +++ b/symlink-images-by-aspect-ratio.pl @@ -7,7 +7,7 @@ use File::Find::Wanted; use File::Basename; use File::Spec; use File::Spec::Functions; -use Image::EXIF; +use Image::Size; use Data::Dumper::Simple; use Math::Round; @@ -59,64 +59,42 @@ if (! @list) { exit 0 } -# extract the EXIF data from the files and build a hash from all that stuff +# Go through all the image files and build the hash that holds all the +# information about them. 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 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 { - $x = $exif->{"Image Width"}; - } - - # 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 + msg "Gathering sizes for image in file: '$f'\n"; + (my $x, my $y) = imgsize($f); + + # calculate aspect ratio my $ar; - if ($error_flag) { - msg "Unable to calculate aspect ratio for image '$f'.\n"; - $ar = "unknown"; + if ($x and $y) { + $ar = nearest(0.1, ($x > $y) ? $x / $y : $y / $x); + $ars->{$ar}++; } else { - $ar = nearest(0.01, ($x > $y) ? $x / $y : $y / $x) unless $error_flag; + $errors++; } - $ars->{$ar}++; + + # append the stuff to hash + $files->{$f}->{x} = $x; + $files->{$f}->{y} = $y; $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 EXIF data of all images +# Dump the hash data if we are in debug mode. if ($conf->{debug}) { - msg_debug "All images and their EXIF data:\n"; + msg_debug "All images and their data:\n"; print Dumper $files; msg_debug "\n"; } -# Dump some statistics -msg "Total images found: $count. Images with invalid EXIF data: $errors\n"; +# Dump some statistics. +msg "Number of images found: $count. Number of images with errors: $errors.\n"; msg "Aspect ratio information for all images (aspect ratio => number of occurences):\n"; print Dumper $ars; @@ -135,8 +113,8 @@ msg "Symlinking source files into destination directories.\n"; foreach my $f (keys %$files) { next unless exists $files->{$f}->{ar}; - # Construct the symlink path. TODO: use something smarter than just string - # concatenation. + # Construct the path of the symlink. This is quite tricky to get right. + # 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' "; @@ -147,7 +125,7 @@ foreach my $f (keys %$files) { ); msg_debug "\$symlink_dst='$symlink_dst' "; msg_debug "\n"; - # symlink + # Create the symlink. msg_debug "symlink($symlink_dst, $symlink);\n"; symlink($symlink_dst, $symlink); }