1#!/usr/local/bin/perl
2
3# sizesort --- sort images to directories according to size.
4
5# Copyright (C) 2003-2006 Mark Probst, 2005 Christoph Weiss-Schaber
6# Copyright (C) 2005 Christoph Weiss-Schaber
7
8# Authors: Mark Probst <schani@complang.tuwien.ac.at>
9#          Christoph Weiss-Schaber <chris@xchaos.de>
10# Maintainer: Mark Probst <schani@complang.tuwien.ac.at>
11
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2, or (at your option)
15# any later version.
16
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, you can either send email to this
24# program's maintainer or write to: The Free Software Foundation,
25# Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.
26
27use strict;
28
29use Getopt::Long;
30
31sub usage {
32    print STDERR "Usage: $0 [OPTION]... <srcdir> <destdir>
33
34Moves all images in <srcdir> to subdirectories of <destdir> (which are
35created on the fly) bearing the name of the respective image's size or
36aspect ratio.
37
38  -h, --help           display this help and exit
39  -d, --delete         automatically deletes images which would normally
40                       be ignored because they are corrupted
41  -s, --size           sort by size
42  -r, --ratio=ACCURACY sort by aspect ratio to ACCURACY decimal places
43  -l, --symlink        symlink instead of move
44";
45    exit(1);
46}
47
48my $delete_images;
49my $sort_size;
50my $sort_ratio;
51my $symlink;
52
53if (!GetOptions("help|h", \&usage,
54		"delete|d", \$delete_images,
55		"size|s", \$sort_size,
56		"ratio|r=i", \$sort_ratio,
57		"symlink|l", \$symlink)) {
58    usage();
59}
60
61if ($#ARGV != 1) {
62    usage();
63}
64
65if (!$sort_size && !$sort_ratio) {
66    print STDERR "$0: you must specify a sorting criterion\n";
67    exit(1);
68}
69
70if ($sort_size && $sort_ratio) {
71    print STDERR "$0: you can only specify one sorting criterion\n";
72    exit(1);
73}
74
75my ($srcdir, $destdir) = @ARGV;
76
77if (! -d $srcdir || ! -r $srcdir) {
78    print "$0: directory $srcdir does not exist or is unreadable\n";
79    exit(1);
80}
81
82if (! -d $destdir ) {
83    print "$0: directory $destdir does not exist\n";
84    exit(1);
85}
86
87foreach my $filename (glob("$srcdir/*")) {
88    if (-f $filename && -r $filename) {
89	my ($w, $h) = split /\s+/, `metapixel-imagesize "$filename" 2>/dev/null`;
90	if ($? != 0 || $w==0 || $h==0) {
91	    if ($delete_images) {
92		`rm "$filename"`;
93		print "warning: metapixel-imagesize failed for for $filename - deleted\n";
94	    } else {
95		print "warning: metapixel-imagesize failed for for $filename - ignored\n";
96	    }
97	} else {
98	    my $dir;
99	    if ($sort_size) {
100		$dir = sprintf "$destdir/size_%dx%d", $w, $h;
101		print "$filename $w $h\n";
102	    } else {
103		my $ratio = sprintf "%.${sort_ratio}f", ($w / $h);
104		print "$filename $w $h $ratio\n";
105		$dir = sprintf "$destdir/ratio_%s", $ratio;
106	    }
107
108	    `mkdir "$dir"` if (!-d $dir);
109	    if ($symlink) {
110		`ln -s "$filename" "$dir/"`;
111	    } else {
112		`mv "$filename" "$dir/"`;
113	    }
114        }
115    }
116}
117