-
Notifications
You must be signed in to change notification settings - Fork 3
/
imagesize
executable file
·49 lines (45 loc) · 1.34 KB
/
imagesize
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
#!/usr/bin/perl
# https://gist.github.com/3246780
use strict;
use 5.10.0;
my ($mx, $my);
unless( @ARGV ) {
say 'imagesize { file | dimension }+';
say '';
say ' Image sizes in pixels and mm are shown for each file.';
say ' Dimensions modify the density of image files (dpi), for instance:';
say ' 12x15mm, 100mm (height calculated), x32mm (width calculated)';
exit 1;
}
sub getsize {
my $file = shift;
my $info = do { # avoid the shell, no escaping needed
my @args = ('-format','%G %x%y','-units','PixelsPerInch',$file);
open my $fh, "-|", "identify", @args or die "$!\n";
<$fh>;
};
if ($info =~ /^(\d+)x(\d+) ([0-9.]+) PixelsPerInch([0-9.]+) PixelsPerInch/) {
my ($px,$py,$dx,$dy) = ($1,$2,$3,$4);
my ($sx,$sy) = map { $_ * 25.4 } ($px/$dx, $py/$dy);
return ($px,$py,$dx,$dy,$sx,$sy);
} else {
die "!$info";
}
}
foreach my $file (@ARGV) {
if ($file =~ /^([0-9.]*)(x([0-9.]+))?mm$/) {
($mx,$my) = ($1,$3);
} elsif( -e $file ) {
my ($w,$h);
if ($mx || $my) {
my ($px,$py,$dx,$dy,$sx,$sy) = getsize($file);
my $rx = 25.4 * ( $mx ? ($px/$mx) : ($py/$my) );
my $ry = 25.4 * ( $my ? ($py/$my) : ($px/$mx) );
system qw(convert -units PixelsPerInch -density),
sprintf("%.0fx%.0f",$rx,$ry), $file, $file;
}
printf "$file: %dx%d at %dx%ddpi = %dx%dmm\n", getsize($file);
} else {
die "file not found: $file\n";
}
}