0

I am trying to create a script using jhead or exiftool to help me fix the metadata issues I got due to an Apple photo export. I am trying to move away from the apple ecosystem but Apple photos didn't just use EXIF to sort photos but also import dates etc. So when I export original photos with EXIF data, they end up in directories containing the date but don't all have exif data.

For example:

/8 February 2011/img - 6676.JPG

Is the only photo in this directory that doesn't have metadata. I would like to add the date (8-2-2011) to the metadata.

Not very good with bash so if anyone can help, I would greatly appreciate.

Aserre
  • 4,916
  • 5
  • 33
  • 56
  • Well, it's doable, but I would go back to whatever exported it into the spaghetti of directories without exif data and see if I could change options to export with metatdata to a directory of my choice. That said, `jhead` is your friend. I have used it a number of times to modify, sort, label, etc.. It is many times faster than exiftool (if I recall that is a perl compilation). You can use `jhead -mkexif` to create an exif header for the image. The rest is parsing the directory from the full filename and running it though date (e.g. `date -d "8 February 2011" +'%m-%d-%Y'`) gives `02-08-2011` – David C. Rankin Oct 25 '17 at 05:54
  • Or I guess you want `date -d "8 February 2011" +'%d-%m-%Y'` for `08-02-2011` – David C. Rankin Oct 25 '17 at 05:56
  • Makes sense, thanks David, I am thinking of grabbing all photos without exif first make a file with it and recursively add just date to the photos that don't have exif data. Thanks for your help :) – Tom Johannes Oct 25 '17 at 06:00

2 Answers2

1

Continuing from the comments, you can easily convert the directory name to a date using date -d in bash. The trick is parsing the directory from the full filename (e.g. /8 February 2011/img - 6676.JPG). Since it is an absolute path, you will have to use string indexes to chop the leading '/' regardless whether you use the dirname command or you simply parse it with bash parameter expansion (which is the simplest here).

Here is a short example showing how to handle the directory you have given. You may need to modify the jhead commands so test it before you just turn any script loose on all of your photos.

#!/bin/bash

f="/8 February 2011/img - 6676.JPG"    ## example filename "$f"

[ "${f:0:1}" = '/' ] && d="${f:1}"     ## check/trim leading '/' for dir "$d"
d="${d%%/*}"                           ## strip filename leaving dir for time in $d
dt="$(date -d "$d" +'%d-%m-%Y')"       ## get date from $d in your format
jhdt="$(date -d "$d" +'%Y:%m:%d')"     ## get date in format required by jhead

## output dates and commands for jhead
printf "file    : \"%s\"\n" "$f"
printf "has date: %s\n" "$dt"
printf "jhead -mkexif \"%s\"\n" "$f"
printf "jhead -ds %s \"%s\"\n" "$jhdt" "$f"

Example Use/Output

$ bash dirtodate.sh
file    : "/8 February 2011/img - 6676.JPG"
has date: 08-02-2011
jhead -mkexif "/8 February 2011/img - 6676.JPG"
jhead -ds 2011:02:08 "/8 February 2011/img - 6676.JPG"
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Hi there, just thanking you again and putting what fixed my whole collection (helped a lot by commenters out there) this is the way to go if you export all your photos from apple photos and want them in Google Photos or plex: – Tom Johannes Oct 26 '17 at 05:09
  • Glad I could help. I had the displeasure of a lot of family photos that were similarly stored and exported (and that sinking feeling of "Oh no.."). So I spent a bit of time with `jhead` a while back and was completely satisfied with what it did. `:)` – David C. Rankin Oct 26 '17 at 05:12
0

Thank you for your help. Here is my "final" dirty script to fix my 25k photo collection exif files, generated by Apple photos and to export to Plex or Google photos:

#!/bin/bash
FILES=/share/Photos/Tom/*/*.jpg
DATE_IMPORT="2017:09:1*"
for f in $FILES
do
  date_exif=`jhead -q "$f" | sed -n -e 's/^.*date //p' | cut -d' ' -f 5`
  #echo $date_exif
  if [[ $date_exif =~ $DATE_IMPORT ]]
     then
        aplevent=`echo "$f" | cut -d '/' -f 5`
        date=`echo "$aplevent" | rev | cut  -d',' -f 1 | rev;`
        date_folder="$(date -d "$date" +'%Y:%m:%d')"
        echo "Date exif: "$date_exif
        echo "Date fix:"$date_folder
        echo "$f"
        jhead -mkexif "$f"
        jhead -ds"$date_folder" "$f"
        jhead -ft "$f"
  fi
done