Initial import
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
links/*
|
||||
pics/*
|
||||
105
symlink-images-by-aspect-ratio.pl
Executable file
105
symlink-images-by-aspect-ratio.pl
Executable file
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
use warnings;
|
||||
use strict;
|
||||
|
||||
use File::Find::Wanted;
|
||||
use File::Basename;
|
||||
use Image::EXIF;
|
||||
use Data::Dumper::Simple;
|
||||
use Math::Round;
|
||||
|
||||
$Data::Dumper::Indent = 3;
|
||||
|
||||
my $conf = {
|
||||
src_dir => "./pics",
|
||||
dst_dir => "./links",
|
||||
};
|
||||
|
||||
# gather the list of files
|
||||
my @list = find_wanted(
|
||||
sub { -f && ( /\.jpg$/i || /\.jpeg$/i ) },
|
||||
$conf->{"src_dir"}
|
||||
);
|
||||
|
||||
# get their information
|
||||
my (%files, %ars, $i, $errors);
|
||||
foreach my $f (@list) {
|
||||
$i++;
|
||||
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;
|
||||
$ars{$ar}++;
|
||||
$files{$f}->{"ar"} = $ar;
|
||||
}
|
||||
|
||||
# Append the exif data into hash
|
||||
$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";
|
||||
|
||||
print "Total images found: $i\n";
|
||||
print "Images with invalid EXIF data: $errors\n";
|
||||
|
||||
print "Aspect ratio information for all images (aspect ratio => number of occurences):\n";
|
||||
print Dumper %ars;
|
||||
print "\n";
|
||||
|
||||
# Create destination directories
|
||||
print "Creating destination directories.\n";
|
||||
foreach my $d (keys %ars) {
|
||||
my $dirname = $conf->{"dst_dir"} . "/" . $d;
|
||||
print "Creating directory: $dirname\n";
|
||||
mkdir $dirname;
|
||||
}
|
||||
print "\n";
|
||||
|
||||
# Symlink
|
||||
print "Symlinking source files into destination directories.";
|
||||
foreach my $f (keys %files) {
|
||||
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;
|
||||
|
||||
# symlink
|
||||
print "$f -> $dest\n";
|
||||
symlink($f, $dest);
|
||||
}
|
||||
|
||||
# vim: set ts=4 sw=4 et cc=80:
|
||||
Reference in New Issue
Block a user