-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexifautotran
executable file
·84 lines (63 loc) · 1.7 KB
/
exifautotran
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
#!/usr/bin/perl
use 5.012;
use warnings;
use Image::ExifTool ();
use File::Find qw/find/;
use IPC::Cmd qw/ can_run /;
my %TRANSFORM_MAP = (
2 => [qw/-flip horizontal/],
3 => [qw/-rotate 180/],
4 => [qw/-flip vertical/],
5 => [qw/-transpose/],
6 => [qw/-rotate 90/],
7 => [qw/-transverse/],
8 => [qw/-rotate 270/],
);
can_run('jpegtran')
or die "jpegtran is not found, try to install graphics/jpeg-turbo or graphics/jpeg\n";
my $exifTool = new Image::ExifTool;
my $dir = $ARGV[0];
usage() unless -d $dir;
find(\&auto_rotate, $dir);
sub auto_rotate {
my $file = $File::Find::name;
return unless defined $file && -f $file;
return unless $file =~ /\.jpg$/i;
die "$file is not writable: $!" unless -w $file;
my $r = $exifTool->ExtractInfo($file, {FastScan => 2});
if ($r == 0) {
warn "can't extract EXIF from $file\n";
return;
}
my $orientation = $exifTool->GetValue('Orientation', 'Raw') or return;
my $transform = $TRANSFORM_MAP{$orientation} or return;
my $tmp = "$file.tmp$$";
# XXX thumbnails are not rotated
my @cmd = ('jpegtran', '-optimize', '-copy', 'all', @$transform, '-outfile', $tmp, $file);
system(@cmd);
if ($? == -1) {
print "failed to execute '@cmd': $!\n";
unlink $tmp;
return;
}
elsif ($? & 127) {
printf "'@cmd' died with signal %d for %s\n",
($? & 127), $file;
unlink $tmp;
return;
}
elsif ($? >> 8) {
printf "'@cmd' exited with value %d for %s\n", $? >> 8, $file;
unlink $tmp;
return;
}
$exifTool->ExtractInfo($tmp);
$exifTool->SetNewValue('IFD0:Orientation' => 1, Type => 'ValueConv');
$exifTool->WriteInfo($tmp);
rename $tmp, $file;
warn "$file was rotated\n";
}
sub usage {
warn "Usage: $0 directory_with_photos_to_roate\n";
exit 1;
}