I am using piexif library to modify the gps altitude of a tif file's EXIF data. This is my implementation:
import piexif
from PIL import Image
Image.MAX_IMAGE_PIXELS = 1000000000
fname_1='initial.tif'
fname_2='new_file.tif'
img = Image.open(fname_1)
exif_dict = piexif.load(fname_1)
new_exif_dict = exif_dict
new_exif_dict['GPS'][piexif.GPSIFD.GPSAltitude] = (140, 1)
del new_exif_dict['0th'][50714] # I delete this because it causes an error for class type for some reason. It happens even if I dump the original metadata as they are, to a new tif file
exif_bytes = piexif.dump(new_exif_dict)
im = Image.open(fname_1)
im.save(fname_2, exif=exif_bytes)
The code works, however the metadata are now a lot less on the new tif photo than the original one. Even the GPS coordinates are lost.
My question is how could I change the tif file's metadata about GPS without affecting the rest?
Using PIL and saving the image will recompress the data and rewrite or remove many tags. You can use a different library that alters less. For instance, if you are only working with tiff files, you can do this with the
tifftoolspython package via the command line:or via python:
Disclaimer: I'm the author of tifftools.