From f617949281e42ff3371e62226d10cb48a0ec8d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uro=C5=A1=20Golja?= Date: Tue, 25 Oct 2016 16:01:24 +0200 Subject: [PATCH] Initial import --- .gitignore | 2 + symlink-images-by-aspect-ratio.pl | 105 ++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 .gitignore create mode 100755 symlink-images-by-aspect-ratio.pl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d8be33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +links/* +pics/* diff --git a/symlink-images-by-aspect-ratio.pl b/symlink-images-by-aspect-ratio.pl new file mode 100755 index 0000000..982f2f6 --- /dev/null +++ b/symlink-images-by-aspect-ratio.pl @@ -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: