Skip to content

Instantly share code, notes, and snippets.

@pichfl
Last active July 9, 2024 07:54
Show Gist options
  • Save pichfl/bdbdc36627f5cf2de8b7fd4a6a54ee9a to your computer and use it in GitHub Desktop.
Save pichfl/bdbdc36627f5cf2de8b7fd4a6a54ee9a to your computer and use it in GitHub Desktop.
Archive Sony Alpha 7C II photos from an SD card.

archive-photos

The script will

  • Convert all ARW files to DNG for compatibility (Apple does not support the 7C II yet and I prefer DNG anyway)
  • Sort the new DNGs into a YYYY/MM/DD based folder structure (and set creation date)
  • Rename *.HIF to *.heic so the pictures are correctly rotated when imported into Apple Photos
  • Move the original files to your Trash

It will not

  • Autoimport to Apple Photos. Scripting with AppleScript or Shortcuts could be an option, but this is fine.
  • Erase the easter eggs from your DVDs

Requirements

Installation

Put all .fish files in `~/.config/fish/functions

Usage

  • Insert SD card. It should contain a DCIM folder at its root level.
  • Run archive-photos from Terminal.
  • Open Apple Photos to import the HEIC files
function archive-photos
set current (pwd)
set volumes (find /Volumes -maxdepth 2 -type d -name 'DCIM')
set dcim (echo $volumes | awk '{print $1}')
cd $dcim
# rename all .HIF and .hif files to .heic
rename-to-heic
set files (find . -type f -name '*.ARW')
if not count $files
echo "No .ARW files found."
return 0
end
# Move all raw files to a temp folder for conversion
mkdir -p ~/.temp/raw
mv $files ~/.temp/raw/
# Convert raw files to dng
cd ~/.temp/raw
convert-raw
# Output: folders named YYYY/MM/DD with dng files
# Archive dng files to /Volumes/photo, merging folders
rsync -av --remove-source-files --ignore-existing --progress . /Volumes/photo
# Remove temp folder
rm -rf ~/.temp/raw
cd $current
end
function convert-raw
set converter "/Applications/Adobe DNG Converter.app/Contents/MacOS/Adobe DNG Converter"
if not test -x "$converter"
echo "Error: Adobe DNG Converter not found at $converter. Download at <https://helpx.adobe.com/camera-raw/using/adobe-dng-converter.html>"
return 1
end
set files (find . -type f -name '*.ARW')
if not count $files
echo "No .ARW files found."
return 0
end
# Sort files by creation date
for file in $files
set creation_date (stat -f %SB -t %Y%m%d%H%M.%S "$file")
set creation_year (stat -f %SB -t %Y "$file")
set creation_month (stat -f %SB -t %m "$file")
set creation_day (stat -f %SB -t %d "$file")
set target_dir "./$creation_year/$creation_month/$creation_day"
mkdir -p "$target_dir"
mv "$file" "$target_dir/"
end
# Retrieve sorted files
set files (find . -type f -name '*.ARW')
# Convert files to dng
"$converter" $files
# Copy creation date and trash original files
for file in $files
set creation_date (stat -f %SB -t %Y%m%d%H%M.%S "$file")
set new_file (echo "$file" | sed 's/\.ARW/\.dng/')
touch -t $creation_date "$new_file"
# brew install trash
trash $file
end
end
function rename-to-heic
set files (find . -type f -name '*.HIF' -o -name '*.hif')
if not count $files
echo "No .HIF or .hif files found."
return 0
end
for file in $files
set new_file (echo "$file" | sed 's/\.HIF/\.heic/' | sed 's/\.hif/\.heic/')
mv "$file" "$new_file"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment