Compare commits

...

10 Commits

Author SHA1 Message Date
Uroš Golja
a84c0d7e55 Ignored some more stuff. 2017-01-05 20:22:16 +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 72 additions and 36 deletions

3
.gitignore vendored
View File

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

View File

@@ -5,52 +5,75 @@ use strict;
use File::Find::Wanted;
use File::Basename;
use File::Spec;
use File::Spec::Functions;
use Image::EXIF;
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
# 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++;
print "Gathering info for image in file: $f\n";
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"}) {
print "WARNING: Could not find attribute 'Image Width'.";
msg "Could not find attribute 'Image Width' for image '$f'.\n";
$error_flag = 1;
}
else {
@@ -59,7 +82,7 @@ foreach my $f (@list) {
# Check for the existence of y
if (!defined $exif->{"Image Height"}) {
print "WARNING: Could not find attribute 'Image Height'.";
msg "Could not find attribute 'Image Height' for image '$f'.\n";
$error_flag = 1;
}
else {
@@ -67,56 +90,66 @@ foreach my $f (@list) {
}
# Calculate aspect ratio and append it into the hash
my $ar;
if ($error_flag) {
print "Unable to calculate aspect ratio for this image. Skipping."
msg "Unable to calculate aspect ratio for image '$f'.\n";
$ar = "unknown";
}
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 "\n";
# Dump the EXIF data of all images
if ($conf->{debug}) {
msg_debug "All images and their EXIF 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 "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;
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 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' ";
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);
msg_debug "symlink($symlink_dst, $symlink);\n";
symlink($symlink_dst, $symlink);
}
# vim: set ts=4 sw=4 et cc=80: