Compare commits
10 Commits
b3f2dd01ba
...
4a92702082
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a92702082 | ||
|
|
b39571ff74 | ||
|
|
3729d244f4 | ||
|
|
9fe9c281ef | ||
|
|
050b0a5802 | ||
|
|
1a7507c6bc | ||
|
|
d85735ef46 | ||
|
|
b947df75ee | ||
|
|
ac2ea74ad5 | ||
|
|
ee25e6d65a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,5 @@
|
||||
*.swp
|
||||
links/*
|
||||
pics/*
|
||||
slike/*
|
||||
linki/*
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user