Skip to content

Instantly share code, notes, and snippets.

@jontwo
Last active May 25, 2020 11:30
Show Gist options
  • Save jontwo/b069df72fe2fda774488c9decb4af922 to your computer and use it in GitHub Desktop.
Save jontwo/b069df72fe2fda774488c9decb4af922 to your computer and use it in GitHub Desktop.
Check if any photos have been geotagged. I usually turn off geotagging for privacy reasons, but realised that one or two of my photos were still tagged. This script scans a directory and reads the EXIF tags of any jpgs in there, logging with ones have a location tag. Requires PIL/Pillow library to be installed.
# -*- coding:utf-8 -*-
"""check_geotags.py.
Scans an image folder and logs which jpgs have GPS tags in the EXIF data.
Requires PIL/Pillow to be installed in the current environment.
"""
import os
import sys
from PIL import Image
def main(srcdir, logfile):
with open(logfile, 'w') as log:
for rr, dd, ff in os.walk(srcdir):
for f in ff:
if os.path.splitext(f)[1].lower() == '.jpg':
img_path = os.path.join(rr, f)
try:
img = Image.open(img_path)
info = img._getexif()
gps = info[34853]
location = (gps[1], gps[2], gps[3], gps[4])
if any(location):
log.write(f"{img_path} {location}\n")
except (OSError, TypeError, KeyError):
pass
if __name__ == '__main__':
try:
main(sys.argv[1], sys.argv[2])
except IndexError:
print("Usage: check_geotags.py <image directory> <log file path>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment