From 1c15c0d5d1904c7d2628b1a091be2ff374212530 Mon Sep 17 00:00:00 2001 From: ed Date: Sun, 2 Nov 2025 13:12:13 +0000 Subject: [PATCH] mtag: add geotag.py --- bin/mtag/README.md | 5 +++++ bin/mtag/geotag.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100755 bin/mtag/geotag.py diff --git a/bin/mtag/README.md b/bin/mtag/README.md index 4656fb05..bc742ef2 100644 --- a/bin/mtag/README.md +++ b/bin/mtag/README.md @@ -68,3 +68,8 @@ instead of affecting all volumes, you can set the options for just one volume li * `:c,mtp=key=f,audio-key.py` * `:c,mtp=.bpm=f,audio-bpm.py` * `:c,mtp=ahash,vhash=f,media-hash.py` + + +# tips & tricks + +* to delete tags for all files below `blog*` and rescan that, `sqlite3 .hist/up2k.db "delete from mt where w in (select substr(w,1,16) from up where rd like 'blog%')";` diff --git a/bin/mtag/geotag.py b/bin/mtag/geotag.py new file mode 100755 index 00000000..dd6df91c --- /dev/null +++ b/bin/mtag/geotag.py @@ -0,0 +1,53 @@ +import json +import re +import sys + +from copyparty.util import fsenc, runcmd + + +""" +uses exiftool to geotag images based on embedded gps coordinates in exif data + +adds four new metadata keys: + .gps_lat = latitute + .gps_lon = longitude + .masl = meters above sea level + city = "city, subregion, region" + +usage: -mtp crc32,md5,sha1,sha256b=ad,bin/mtag/cksum.py + +example: https://a.ocv.me/pub/blog/j7/8/?grid=0 +""" + + +def main(): + cmd = b"exiftool -api geolocation -n".split(b" ") + rc, so, se = runcmd(cmd + [fsenc(sys.argv[1])]) + ptn = re.compile("([^:]*[^ :]) *: (.*)") + city = ["", "", ""] + ret = {} + for ln in so.split("\n"): + m = ptn.match(ln) + if not m: + continue + k, v = m.groups() + if k == "Geolocation City": + city[2] = v + elif k == "Geolocation Subregion": + city[1] = v + elif k == "Geolocation Region": + city[0] = v + elif k == "GPS Latitude": + ret[".gps_lat"] = "%.04f" % (float(v),) + elif k == "GPS Longitude": + ret[".gps_lon"] = "%.04f" % (float(v),) + elif k == "GPS Altitude": + ret[".masl"] = str(int(float(v))) + v = ", ".join(city).strip(", ") + if v: + ret["city"] = v + print(json.dumps(ret)) + + +if __name__ == "__main__": + main()