Skip to content

Commit 9ffa728

Browse files
committed
modified: Image/add_exif_info.pl -- extended to support utime()
1 parent 453adc6 commit 9ffa728

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

Image/add_exif_info.pl

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,71 @@
22

33
# Author: Trizen
44
# Date: 30 September 2024
5+
# Edit: 24 September 2025
56
# https://github.com/trizen
67

7-
# Add the EXIF "DateTimeOriginal" to images, based on the filename of the image, along with custom GPS tags.
8+
# Add the EXIF "DateTimeOriginal" to images, based on the filename of the image, with support for GPS tags.
89

910
use 5.036;
1011
use Image::ExifTool qw();
12+
use File::Find qw(find);
1113
use Getopt::Long qw(GetOptions);
14+
use Time::Piece qw();
15+
16+
my $latitude = 45.84692326942804;
17+
my $longitude = 22.796479967835673;
1218

13-
my $latitude = 45.84692326942804;
14-
my $longitude = 22.796479967835673;
1519
my $coordinates = undef;
20+
my $set_gps = 0;
21+
my $utc_offset = 0;
22+
23+
my $img_formats = '';
24+
25+
my @img_formats = qw(
26+
jpeg
27+
jpg
28+
);
1629

1730
sub usage($exit_code = 0) {
1831

1932
print <<"EOT";
2033
usage: $0 [options] [images]
2134
2235
options:
23-
36+
--gps! : set the GPS coordinates
2437
--latitude=float : value for GPSLatitude
2538
--longitude=float : value for GPSLongitude
2639
--coordinates=str : GPS coordinates as "latitude,longitude"
40+
--UTC-offset=i : offset date by this many hours (default: $utc_offset)
41+
-f --formats=s,s : specify more image formats (default: @img_formats)
2742
--help : print this message and exit
2843
EOT
2944

3045
exit $exit_code;
3146
}
3247

3348
GetOptions(
49+
"gps!" => \$set_gps,
50+
"f|formats=s" => \$img_formats,
51+
"utc-offset=i" => \$utc_offset,
3452
"latitude=f" => \$latitude,
3553
"longitude=f" => \$longitude,
3654
"coordinates=s" => \$coordinates,
3755
'help' => sub { usage(0) }
3856
)
3957
or die("Error in command line arguments\n");
4058

41-
@ARGV or usage(2);
42-
4359
if (defined($coordinates)) {
4460
($latitude, $longitude) = split(/\s*,\s*/, $coordinates);
4561
}
4662

47-
foreach my $file (@ARGV) {
48-
49-
say "Processing: $file";
63+
sub process_image ($file) {
5064

5165
my $exifTool = Image::ExifTool->new;
66+
5267
$exifTool->ExtractInfo($file);
5368

54-
if ($file =~ m{.*IMG_((?:20|19)[0-9]{2})([0-9]{2})([0-9]{2})_([0-9]{2})([0-9]{2})([0-9]{2})}) {
69+
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})}) {
5570
my ($year, $month, $day, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6);
5671

5772
my $date = "$year:$month:$day $hour:$min:$sec";
@@ -66,14 +81,57 @@ ($exit_code = 0)
6681
}
6782

6883
# Set GPSLatitude and GPSLongitude tags
69-
$exifTool->SetNewValue('GPSLatitude', $latitude);
70-
$exifTool->SetNewValue('GPSLatitudeRef', $latitude >= 0 ? 'N' : 'S');
71-
$exifTool->SetNewValue('GPSLongitude', $longitude);
72-
$exifTool->SetNewValue('GPSLongitudeRef', $longitude >= 0 ? 'E' : 'W');
84+
if ($set_gps) {
85+
$exifTool->SetNewValue('GPSLatitude', $latitude);
86+
$exifTool->SetNewValue('GPSLatitudeRef', $latitude >= 0 ? 'N' : 'S');
87+
$exifTool->SetNewValue('GPSLongitude', $longitude);
88+
$exifTool->SetNewValue('GPSLongitudeRef', $longitude >= 0 ? 'E' : 'W');
89+
}
90+
91+
my $time_obj = Time::Piece->strptime($date, "%Y:%m:%d %H:%M:%S");
92+
93+
if ($utc_offset) {
94+
$time_obj += $utc_offset * 3600;
95+
}
96+
97+
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
7398

7499
$exifTool->WriteInfo($file);
100+
101+
$mtime = $time_obj->epoch;
102+
$atime = $mtime;
103+
104+
# Set the original ownership of the image
105+
chown($uid, $gid, $file);
106+
107+
# Set the modification time
108+
utime($atime, $mtime, $file)
109+
or warn "Can't change timestamp: $!\n";
110+
111+
# Set original permissions
112+
chmod($mode & 07777, $file)
113+
or warn "Can't change permissions: $!\n";
75114
}
76115
else {
77116
warn "Unable to determine the image creation date. Skipping...\n";
78117
}
118+
79119
}
120+
121+
@ARGV || usage(1);
122+
123+
push @img_formats, map { quotemeta } split(/\s*,\s*/, $img_formats);
124+
125+
my $img_formats_re = do {
126+
local $" = '|';
127+
qr/\.(@img_formats)\z/i;
128+
};
129+
130+
find {
131+
no_chdir => 1,
132+
wanted => sub {
133+
(/$img_formats_re/o && -f) || return;
134+
say ":: Processing: $_";
135+
process_image($_);
136+
}
137+
} => @ARGV;

0 commit comments

Comments
 (0)