From bc97ae182f9e04431bb60b9f5c53b5e4d3c2a7af Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 24 Mar 2025 22:16:51 -0700 Subject: [PATCH] Add function remove_geolocation. --- voussoirkit/imagetools.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/voussoirkit/imagetools.py b/voussoirkit/imagetools.py index e2aaf0b..7eaa233 100644 --- a/voussoirkit/imagetools.py +++ b/voussoirkit/imagetools.py @@ -155,6 +155,34 @@ def pad_to_square(image, background_color=None) -> PIL.Image: new_image.paste(image, (diff_w, diff_h)) return new_image +def remove_geolocation(image, exif=None): + if exif is None: + try: + exif = image.getexif() + except AttributeError: + return (image, exif) + + if exif is None: + return (image, exif) + + fp = getattr(exif, 'fp', None) + if isinstance(fp, io.BufferedReader): + exif.fp = io.BytesIO() + exif.fp.write(fp.read()) + exif.fp.seek(0) + exif = copy.deepcopy(exif) + + print(exif) + for (key, value) in PIL.ExifTags.GPSTAGS.items(): + print(key) + if key in exif: + exif.pop(key) + + if GEOLOCATION_KEY in exif: + exif.pop(GEOLOCATION_KEY) + + return (image, exif) + def replace_color(image, from_color, to_color): image = image.copy() pixels = image.load()