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