|
| 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; |
0 commit comments