Compare commits

...

10 Commits

Author SHA1 Message Date
Uroš Golja
4a92702082 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.
2017-01-05 20:18:57 +01:00
Uroš Golja
b39571ff74 Some more ignores.. 2017-01-05 19:58:47 +01:00
Uroš Golja
3729d244f4 Added "unknown" as a possible apsect ratio. 2016-11-13 22:36:35 +01:00
Uroš Golja
9fe9c281ef Added vim swap files to the list of ignored files. 2016-11-13 22:35:32 +01:00
Uroš Golja
050b0a5802 Added debug mode, modified some outputs. 2016-11-13 21:40:33 +01:00
Uroš Golja
1a7507c6bc This apparently works. The trick was to use the $symlink_dir path as the
basepath (for the abs2rel function), and not $symlink. Kinda makes
sense, really.
2016-11-12 12:25:41 +01:00
Uroš Golja
d85735ef46 This works, dont know why 2016-11-11 16:07:37 +01:00
Uroš Golja
b947df75ee Trying to figure out how this File::Spec and Cwd thing works. 2016-10-28 08:30:52 +02:00
Uroš Golja
ac2ea74ad5 Oops, a copy/paste bug. 2016-10-26 07:51:10 +02:00
Uroš Golja
ee25e6d65a Mostly beauty fixes. 2016-10-25 23:10:05 +02:00
2 changed files with 80 additions and 66 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,5 @@
*.swp
links/*
pics/*
slike/*
linki/*

View File

@@ -5,118 +5,129 @@ use strict;
use File::Find::Wanted;
use File::Basename;
use Image::EXIF;
use File::Spec;
use File::Spec::Functions;
use Image::Size;
use Data::Dumper::Simple;
use Math::Round;
my $conf = {
src_dir => "./pics",
dst_dir => "./links",
src_dir => "./slike",
dst_dir => "./linki",
debug => 0,
};
sub msg {
my $text = shift;
print $text;
}
sub msg_error {
my $text = shift;
print STDERR "ERROR: $text";
}
sub msg_warn {
my $text = shift;
print STDERR "warning: $text";
}
sub msg_debug {
my $text = shift;
print "$text" if $conf->{debug};
}
# verify the source dir
if (! -d $conf->{src_dir}) {
print "ERROR: Source directory '$conf->{src_dir}' does not exist. Aborting.\n" ;
msg_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";
msg_error "Destination 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"}
$conf->{src_dir}
);
if (! @list) {
print "No files found.\n";
msg "No files found.\n";
exit 0
}
# get their information
# 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++;
print "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"}) {
print "WARNING: Could not find attribute 'Image Width'.";
$error_flag = 1;
}
else {
$x = $exif->{"Image Width"};
}
# Check for the existence of y
if (!defined $exif->{"Image Height"}) {
print "WARNING: Could not find attribute 'Image Height'.";
$error_flag = 1;
}
else {
$y = $exif->{"Image Height"};
}
# Calculate aspect ratio and append it into the hash
if ($error_flag) {
print "Unable to calculate aspect ratio for this image. Skipping."
}
else {
my $ar;
$ar = nearest(0.01, ($x > $y) ? $x / $y : $y / $x) unless $error_flag;
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}++;
$files->{$f}->{"ar"} = $ar;
}
else {
$errors++;
}
# Append the exif data into hash
$files->{$f}->{"exif"} = $exif;
# bump the number of errors
$errors++ if $error_flag;
# append the stuff to hash
$files->{$f}->{x} = $x;
$files->{$f}->{y} = $y;
$files->{$f}->{ar} = $ar;
}
print "All images and their EXIF data:";
print Dumper $files;
print "\n";
# Dump the hash data if we are in debug mode.
if ($conf->{debug}) {
msg_debug "All images and their data:\n";
print Dumper $files;
msg_debug "\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";
# 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;
print "\n";
# Create destination directories
print "Creating destination directories: ";
# Create destination directories. TODO: use something smarter than just string
# concatenation.
msg_debug "Creating destination directories: ";
foreach my $d (keys %$ars) {
my $dirname = $conf->{"dst_dir"} . "/" . $d;
print "'$dirname' ";
my $dirname = $conf->{dst_dir} . "/" . $d;
msg_debug "'$dirname' ";
mkdir $dirname;
}
print "\n";
msg_debug "\n";
# Symlink
print "Symlinking source files into destination directories.\n";
msg "Symlinking source files into destination directories.\n";
foreach my $f (keys %$files) {
next unless exists $files->{$f}->{"ar"};
next unless exists $files->{$f}->{ar};
# construct the symlink name
my $name = basename($f);
my $ar = $files->{$f}->{"ar"};
my $dest = $conf->{"dst_dir"} . "/" . $ar . "/" . $name;
# 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' ";
my $symlink = $symlink_dir . "/" . basename($f); msg_debug "\$symlink='$symlink' ";
my $symlink_dst = File::Spec->abs2rel(
File::Spec->rel2abs($f),
File::Spec->rel2abs($symlink_dir)
); msg_debug "\$symlink_dst='$symlink_dst' ";
msg_debug "\n";
# symlink
print "$f -> $dest\n";
symlink($f, $dest);
# Create the symlink.
msg_debug "symlink($symlink_dst, $symlink);\n";
symlink($symlink_dst, $symlink);
}
# vim: set ts=4 sw=4 et cc=80: