Skip to content

Commit 617186f

Browse files
committed
new file: Image/image_metadata_clone.pl
1 parent 2f72cba commit 617186f

File tree

3 files changed

+119
-11
lines changed

3 files changed

+119
-11
lines changed

Image/image_metadata_clone.pl

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/perl
2+
3+
# Author: Trizen
4+
# Date: 26 September 2025
5+
# https://github.com/trizen
6+
7+
# Copy EXIF metadata from images, given a source directory and a destination directory.
8+
9+
# Metadata from each image from the source directory is added to the images
10+
# in the destination directory, based on the filename of each image.
11+
12+
use 5.036;
13+
use Image::ExifTool qw();
14+
use File::Find qw(find);
15+
use File::Basename qw(basename);
16+
use Getopt::Long qw(GetOptions);
17+
18+
my $img_formats = '';
19+
20+
my @img_formats = qw(
21+
jpeg
22+
jpg
23+
);
24+
25+
sub usage($exit_code = 0) {
26+
27+
print <<"EOT";
28+
usage: $0 [options] [source dir] [dest dir]
29+
30+
options:
31+
-f --formats=s,s : specify more image formats (default: @img_formats)
32+
--help : print this message and exit
33+
EOT
34+
35+
exit $exit_code;
36+
}
37+
38+
GetOptions("f|formats=s" => \$img_formats,
39+
'help' => sub { usage(0) })
40+
or die("Error in command line arguments\n");
41+
42+
@ARGV == 2 or usage(1);
43+
44+
sub add_exif_info($source_image, $dest_image) {
45+
46+
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($source_image);
47+
48+
my $exifTool = Image::ExifTool->new;
49+
my $exif_info = $exifTool->SetNewValuesFromFile($source_image);
50+
51+
$exifTool = Image::ExifTool->new;
52+
53+
foreach my $key (keys %$exif_info) {
54+
my $value = $exif_info->{$key};
55+
$exifTool->SetNewValue($key, $value);
56+
}
57+
58+
$exifTool->WriteInfo($dest_image);
59+
60+
# Set the original modification time
61+
utime($atime, $mtime, $dest_image)
62+
or warn "Can't change timestamp: $!\n";
63+
64+
# Set original permissions
65+
chmod($mode & 07777, $dest_image)
66+
or warn "Can't change permissions: $!\n";
67+
68+
# Set the original ownership of the image
69+
chown($uid, $gid, $dest_image);
70+
}
71+
72+
push @img_formats, map { quotemeta } split(/\s*,\s*/, $img_formats);
73+
74+
my $img_formats_re = do {
75+
local $" = '|';
76+
qr/\.(@img_formats)\z/i;
77+
};
78+
79+
my ($source_dir, $dest_dir) = @ARGV;
80+
81+
my %source_files;
82+
83+
find {
84+
no_chdir => 1,
85+
wanted => sub {
86+
(/$img_formats_re/o && -f) || return;
87+
my $basename = basename($_);
88+
$source_files{$basename} = $_;
89+
}
90+
} => $source_dir;
91+
92+
find {
93+
no_chdir => 1,
94+
wanted => sub {
95+
(/$img_formats_re/o && -f) || return;
96+
97+
my $basename = basename($_);
98+
99+
if (exists($source_files{$basename})) {
100+
say "Adding EXIF metadata to: $_";
101+
add_exif_info($source_files{$basename}, $_);
102+
}
103+
else {
104+
warn "Couldn't find <<$basename>> into source directory. Skipping...\n";
105+
}
106+
}
107+
} => $dest_dir;

Image/resize_images.pl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,8 @@ ($image)
163163
return;
164164
};
165165

166-
# Set the original ownership of the image
167-
chown($uid, $gid, $image);
168-
169166
if ($preserve_attr) {
170167

171-
# Set the original modification time
172-
utime($atime, $mtime, $image)
173-
or warn "Can't change timestamp: $!\n";
174-
175-
# Set original permissions
176-
chmod($mode & 07777, $image)
177-
or warn "Can't change permissions: $!\n";
178-
179168
$exifTool = Image::ExifTool->new;
180169

181170
foreach my $key (keys %$exif_info) {
@@ -184,8 +173,19 @@ ($image)
184173
}
185174

186175
$exifTool->WriteInfo($image);
176+
177+
# Set the original modification time
178+
utime($atime, $mtime, $image)
179+
or warn "Can't change timestamp: $!\n";
180+
181+
# Set original permissions
182+
chmod($mode & 07777, $image)
183+
or warn "Can't change permissions: $!\n";
187184
}
188185

186+
# Set the original ownership of the image
187+
chown($uid, $gid, $image);
188+
189189
return 1;
190190
}
191191

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ A nice collection of day-to-day Perl scripts.
550550
* [Image2mozaic](./Image/image2mozaic.pl)
551551
* [Image2png](./Image/image2png.pl)
552552
* [Image2prime](./Image/image2prime.pl)
553+
* [Image metadata clone](./Image/image_metadata_clone.pl)
553554
* [Imager similar images](./Image/imager_similar_images.pl)
554555
* [Img-autocrop](./Image/img-autocrop.pl)
555556
* [Img-autocrop-avg](./Image/img-autocrop-avg.pl)

0 commit comments

Comments
 (0)