There are some images that do not have any EXIF data available. This

happens (sometimes) when the image was edited somehow in an editing program, and
the program forgot to append the original EXIF data to the exported
image.

This is why I've changed the way the size (x, y) of an image is
acquired. Instead of looking at the EXIF data, I'm using a module that
actually READS the size from the image. Kudos to the author.
This commit is contained in:
Uroš Golja
2017-01-05 20:18:57 +01:00
parent b39571ff74
commit 4a92702082
2 changed files with 23 additions and 44 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
links/* links/*
pics/* pics/*
slike/* slike/*
linki/*

View File

@@ -7,7 +7,7 @@ use File::Find::Wanted;
use File::Basename; use File::Basename;
use File::Spec; use File::Spec;
use File::Spec::Functions; use File::Spec::Functions;
use Image::EXIF; use Image::Size;
use Data::Dumper::Simple; use Data::Dumper::Simple;
use Math::Round; use Math::Round;
@@ -59,64 +59,42 @@ if (! @list) {
exit 0 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 $count = 0; # how many files?
my $errors = 0; # how many errors? my $errors = 0; # how many errors?
my $ars = {}; # a hashref that holds all the encountered aspect rations and their number of occurence 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 my $files = {}; # a hashref that holds all the files, their exif data, and their calculcated aspect ratio
foreach my $f (@list) { foreach my $f (@list) {
$count++; $count++;
msg "Gathering info for image in file: '$f'\n"; msg "Gathering sizes for image in file: '$f'\n";
my $e = Image::EXIF->new($f); (my $x, my $y) = imgsize($f);
my $exif = $e->get_image_info();
# calculate aspect ratio
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
my $ar; my $ar;
if ($error_flag) { if ($x and $y) {
msg "Unable to calculate aspect ratio for image '$f'.\n"; $ar = nearest(0.1, ($x > $y) ? $x / $y : $y / $x);
$ar = "unknown"; $ars->{$ar}++;
} }
else { 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; $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}) { if ($conf->{debug}) {
msg_debug "All images and their EXIF data:\n"; msg_debug "All images and their data:\n";
print Dumper $files; print Dumper $files;
msg_debug "\n"; msg_debug "\n";
} }
# Dump some statistics # Dump some statistics.
msg "Total images found: $count. Images with invalid EXIF data: $errors\n"; 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"; msg "Aspect ratio information for all images (aspect ratio => number of occurences):\n";
print Dumper $ars; print Dumper $ars;
@@ -135,8 +113,8 @@ msg "Symlinking source files into destination directories.\n";
foreach my $f (keys %$files) { foreach my $f (keys %$files) {
next unless exists $files->{$f}->{ar}; next unless exists $files->{$f}->{ar};
# Construct the symlink path. TODO: use something smarter than just string # Construct the path of the symlink. This is quite tricky to get right.
# concatenation. # TODO: use something smarter than just string concatenation.
msg_debug "\$f='$f' "; msg_debug "\$f='$f' ";
my $ar = $files->{$f}->{ar}; msg_debug "\$ar='$ar' "; my $ar = $files->{$f}->{ar}; msg_debug "\$ar='$ar' ";
my $symlink_dir = $conf->{dst_dir} . "/" . $ar; msg_debug "\$symlink_dir='$symlink_dir' "; 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 "\$symlink_dst='$symlink_dst' ";
msg_debug "\n"; msg_debug "\n";
# symlink # Create the symlink.
msg_debug "symlink($symlink_dst, $symlink);\n"; msg_debug "symlink($symlink_dst, $symlink);\n";
symlink($symlink_dst, $symlink); symlink($symlink_dst, $symlink);
} }