Skip to content

Instantly share code, notes, and snippets.

Created January 8, 2020 00:26
What would you like to do?
Get all the "Keywords", "Subject" and "TagsList" tags from my photos and save them out as a YAML file
#!/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.
or die "chdir: $!";
my %tags;
while (<STDIN>) {
my $fname = $_;
if (! -r $fname) {
die "Can't read: $fname";
my $exiftool = new Image::ExifTool;
$exiftool->Options(CharsetEXIF => 'UTF8');
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;
This results in a YAML file like…
- Hatter
- Pets
- Pets
- Pets/Hatter
- Bedfont Lakes
- Feltham
- London
- Mandy
- Pets
- Places
- Pets
- Pets/Mandy
- Places
- Places/London
- Places/London/Feltham
- Places/London/Feltham/Bedfont Lakes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment