diff --git a/content/knowledge-base/modify-image-timestamp.md b/content/knowledge-base/modify-image-timestamp.md new file mode 100644 index 0000000..a79a413 --- /dev/null +++ b/content/knowledge-base/modify-image-timestamp.md @@ -0,0 +1,67 @@ ++++ +date = '2026-03-10T18:37:42+01:00' +draft = false +title = 'Modify Image Timestamp' +type='post' +tags = ['Linux', 'Photography'] ++++ +Some cameras don't automatically update their internal clock. This leads to images with "wrong" timestamps. +Modifying the [file metadata](https://en.wikipedia.org/wiki/Metadata) as well as the [Exif metadata](https://en.wikipedia.org/wiki/Exif) can fix this problem. + +## Modifying the file metadata +```bash +touch -m -d "" "" +``` +- `-m` only modify the modification date +- `-d ""` use `` instead of the current date +- `` name of the file to be modified + +### Example Script for adding an offset +The following command adds 20 years and 7 months while subtracting 6 days. +```bash +#!/bin/bash +original_mtime=$(stat -c %y "") +new_mtime=$(date -d "$original_mtime +20 years +7 months -6 days" '+%Y-%m-%d %H:%M:%S') +touch -m -d "$new_mtime" "" +``` +- `stat -c %y ""` reads the current modification timestamp from the file +- `date -d ...` calculates the new date + +## Modifying the Exif metadata +```bash +exiftool -P -overwrite_original "-allDates=" "" +``` +- `exiftool` [FOSS](https://en.wikipedia.org/wiki/Free_and_open-source_software) tool for viewing and modifying Exif data ([source](https://exiftool.org/)) +- `-P` to not overwrite the file modification time (file metadata) +- `allDates=...` modifies the exif metadata dates + - can be used with operators (i.e. `+=`, `-=`) for offsets + +### Example Script for adding an offset +The following command adds 20 years and 7 months while subtracting 6 days. +```bash +#!/bin/bash +exiftool -P -overwrite_original "-allDates+=20:7:0 0:0:0" "" +exiftool -P -overwrite_original "-allDates-=0:0:6 0:0:0" "" +``` + +## Example Script for Combined Modification +This script modifies all `.JPG` files in a given directory by adding 20 years and 7 months while subtracting 6 days. +```bash +#!/bin/bash + +find "" -type f -iname "*.JPG" -print0 | while IFS= read -r -d '' file; do + echo "Processing $file" + + #update meta data + original_mtime=$(stat -c %y "$file") + new_mtime=$(date -d "$original_mtime +20 years +7 months -6 days" '+%Y-%m-%d %H:%M:%S') + touch -m -d "$new_mtime" "$file" + + #update exif data + exiftool -P -overwrite_original "-allDates+=20:7:0 0:0:0" "$file" + exiftool -P -overwrite_original "-allDates-=0:0:6 0:0:0" "$file" +done + +echo "DONE!" +``` +- `` name of the directory to be modified