Using pre-commit to strip Exif data from images before pushing to a Git server
Images can contain a lot of metadata in so called Exif tags, e.g. GPS location, especially when taken using a modern smartphone. When uploading images on my blog, I don't want them to include more metadata than necessary. Thankfully, there exists the great ExifTool which is a package available on many Linux distros and macOS.
ExifTool can be used to view Exif tags but also edit and delete them. The goal is to remove all Exif tags except ICC profiles which contain color information and also keep the orientation of the image.
First, all tags are removed (-all=) except the ICC profiles (--icc_profile:all). Second, the orientation is copied from the original image (-tagsfromfile @ -orientation). This is explained in the documentation:
[I]ndividual tags may be recovered using the -tagsFromFile option (eg.
-all= -tagsfromfile @ -artist).
Last, the original image shall be overwritten (-overwrite_original) so that it won't be checked into Git.
The final command is:
exiftool -all= --icc_profile:all -tagsfromfile @ -orientation -overwrite_original img.jpeg
To automate the execution of the command, I use pre-commit. It generates Git hooks based on the .pre-commit-config.yaml file in the repository. I configured the command be run on every image except SVG files inside the content/ directory:
repos:
- repo: local
hooks:
- id: exiftool
name: ExifTool
description: Remove Exif data from images.
language: system
entry: exiftool -all= --icc_profile:all -tagsfromfile @ -orientation -overwrite_original
exclude_types: ["svg"]
types: ["image"]
files: "^content/"
The Git hook can be installed using pre-commit install and will be executed on the next git commit.
ExifTool.............................................(no files to check)Skipped