Instantly share code, notes, and snippets.
Created
January 8, 2020 00:26
Get all the "Keywords", "Subject" and "TagsList" tags from my photos and save them out as a YAML file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Encode qw(decode_utf8); | |
@ARGV = map { decode_utf8($_, 1) } @ARGV; | |
use Image::ExifTool qw(:Public); | |
use YAML::XS; | |
use Array::Utils qw(:all); | |
use List::MoreUtils qw(uniq); | |
use Data::Dumper; | |
# This is the directory from my backups where known-good copies of the photos are to be found. | |
chdir('/backups/weekly.3/specialbrew.21tc.bitfolk.com/srv/tank/Photos/Andy') | |
or die "chdir: $!"; | |
my %tags; | |
while (<STDIN>) { | |
my $fname = $_; | |
chomp($fname); | |
if (! -r $fname) { | |
die "Can't read: $fname"; | |
} | |
my $exiftool = new Image::ExifTool; | |
$exiftool->Options(CharsetEXIF => 'UTF8'); | |
$exiftool->ExtractInfo($fname); | |
my @keywords = $exiftool->GetValue('Keywords', 'ValueConv'); | |
my @subjects = $exiftool->GetValue('Subject', 'ValueConv'); | |
my @tagslist = $exiftool->GetValue('TagsList', 'ValueConv'); | |
# Safety check just in case list from "keywords" doesn't match list from "Subjects". In my case every single one matched. | |
if (scalar @keywords | |
and scalar @subjects | |
and array_diff(@keywords, @subjects)) { | |
print STDERR Dumper(\@keywords); | |
print STDERR Dumper(\@subjects); | |
die "$fname: keywords and subjects differ"; | |
} | |
my @merged = uniq @keywords, @subjects; | |
$tags{$fname}->{keywords} = \@merged; | |
$tags{$fname}->{tagslist} = \@tagslist; | |
} | |
print Dump(\%tags); | |
exit 0; | |
=pod | |
This results in a YAML file like… | |
--- | |
2011/01/16/16012011163.jpg: | |
keywords: | |
- Hatter | |
- Pets | |
tagslist: | |
- Pets | |
- Pets/Hatter | |
[…] | |
2019/11/29/20191129_095218~2.jpg: | |
keywords: | |
- Bedfont Lakes | |
- Feltham | |
- London | |
- Mandy | |
- Pets | |
- Places | |
tagslist: | |
- Pets | |
- Pets/Mandy | |
- Places | |
- Places/London | |
- Places/London/Feltham | |
- Places/London/Feltham/Bedfont Lakes | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment