-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathadd_exif_info.pl
More file actions
143 lines (108 loc) · 3.97 KB
/
add_exif_info.pl
File metadata and controls
143 lines (108 loc) · 3.97 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl
# Author: Trizen
# Date: 30 September 2024
# Edit: 24 September 2025
# https://github.com/trizen
# Add the EXIF "DateTimeOriginal" to images, based on the filename of the image, with support for GPS tags.
use 5.036;
use Image::ExifTool qw();
use File::Find qw(find);
use Getopt::Long qw(GetOptions);
use Time::Piece qw();
my $latitude = 45.84692326942804;
my $longitude = 22.796479967835673;
my $coordinates = undef;
my $force = 0;
my $set_gps = 0;
my $utc_offset = 0;
my $img_formats = '';
my @img_formats = qw(
jpeg
jpg
);
sub usage($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [images]
options:
--gps! : set the GPS coordinates
--force! : overwrite the EXIF creation date
--latitude=float : value for GPSLatitude
--longitude=float : value for GPSLongitude
--coordinates=str : GPS coordinates as "latitude,longitude"
--UTC-offset=i : offset date by this many hours (default: $utc_offset)
-f --formats=s,s : specify more image formats (default: @img_formats)
--help : print this message and exit
EOT
exit $exit_code;
}
GetOptions(
"gps!" => \$set_gps,
"force!" => \$force,
"f|formats=s" => \$img_formats,
"utc-offset=i" => \$utc_offset,
"latitude=f" => \$latitude,
"longitude=f" => \$longitude,
"coordinates=s" => \$coordinates,
'help' => sub { usage(0) }
)
or die("Error in command line arguments\n");
if (defined($coordinates)) {
($latitude, $longitude) = split(/\s*,\s*/, $coordinates);
}
sub process_image ($file) {
my $exifTool = Image::ExifTool->new;
$exifTool->ExtractInfo($file);
if ($file =~ m{.*(?:/|\D_|\b)((?:20|19)[0-9]{2})([0-9]{2})([0-9]{2})_([0-9]{2})([0-9]{2})([0-9]{2})}) {
my ($year, $month, $day, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6);
my $date = "$year:$month:$day $hour:$min:$sec";
my $time_format = "%Y:%m:%d %H:%M:%S";
my $time_obj = Time::Piece->strptime($date, $time_format);
if ($utc_offset) {
$time_obj += $utc_offset * 3600;
$date = $time_obj->strftime($time_format);
}
say "Setting image creation time to: $date";
# Set the file modification date
$exifTool->SetNewValue(FileModifyDate => $date, Protected => 1);
# Set the EXIF creation date (unless it already exists)
if ($force or not defined $exifTool->GetValue("DateTimeOriginal")) {
$exifTool->SetNewValue(DateTimeOriginal => $date);
}
# Set GPSLatitude and GPSLongitude tags
if ($set_gps) {
$exifTool->SetNewValue('GPSLatitude', $latitude);
$exifTool->SetNewValue('GPSLatitudeRef', $latitude >= 0 ? 'N' : 'S');
$exifTool->SetNewValue('GPSLongitude', $longitude);
$exifTool->SetNewValue('GPSLongitudeRef', $longitude >= 0 ? 'E' : 'W');
}
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
$exifTool->WriteInfo($file);
$mtime = $time_obj->epoch;
$atime = $mtime;
# Set the original ownership of the image
chown($uid, $gid, $file);
# Set the modification time
utime($atime, $mtime, $file)
or warn "Can't change timestamp: $!\n";
# Set original permissions
chmod($mode & 07777, $file)
or warn "Can't change permissions: $!\n";
}
else {
warn "Unable to determine the image creation date. Skipping...\n";
}
}
@ARGV || usage(1);
push @img_formats, map { quotemeta } split(/\s*,\s*/, $img_formats);
my $img_formats_re = do {
local $" = '|';
qr/\.(@img_formats)\z/i;
};
find {
no_chdir => 1,
wanted => sub {
(/$img_formats_re/o && -f) || return;
say ":: Processing: $_";
process_image($_);
}
} => @ARGV;