0

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?

2
  • Would it work if you started with new_exif_dict being a full copy of exif_dict where you only change the fields you want to modify, instead of creating your new_exif_dict by explicitly listing the field you currently initialize it with? Commented Mar 31, 2021 at 12:26
  • Just tried it but many of the metadata are still lost. However, I just updated the code with your suggestion as it's much simpler. I was fooled from documentation! Commented Mar 31, 2021 at 12:31

2 Answers 2

1

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 tifftools python package via the command line:

tifftools set --set GPSAltitude,0,GPSIFD:0 140,1 IMG_0036_1.tif new_file.tif 

or via python:

import tifftools # Load all tag information from the file info = tifftools.read_tiff('IMG_0036_1.tif') # Get a reference to the IFD with GPS tags. If the GPS data is in a different # location, you might need to change this. gpsifd = info['ifds'][0]['tags'][tifftools.Tag.GPSIFD.value]['ifds'][0][0] # Set the altitude tag; this assumes it already exists and is stored as a rational gpsifd['tags'][tifftools.constants.GPSTag.GPSAltitude.value]['data'] = [140, 1] # Write the output; this copies all image data from the original file. tifftools.write_tiff(info, 'new_file.tif') 

Disclaimer: I'm the author of tifftools.

Sign up to request clarification or add additional context in comments.

6 Comments

Works like a charm! And I don't notice any metadata loss either. Thank you!
May I ask, how could I update the same tif file's data? Above code doesn't seem to work with the same tif file.
I've just noticed the allowExisting=True on write_tif but for some reason it results in never-ending execution for some reason
tifftools always writes the entire file, so it has to write to a different file that the source. The allowExisting` flag let's it overwrite an existing output file (but not the same as the input). The higher level commands write to a new temporary file and then replace the original file with that. The tifftools.commands.tiff_set function, for instance, is the equivalent of the tiff set ... command line and will handle this.
Just to be clear, you are saying it is not possible to update a file's existing exif data, right?
|
1

Try tifffile to modify the file in-place:

import tifffile # open the TIFF file for reading and writing with tifffile.TiffFile(filename, mode='r+b') as tif: # get the GPS tag of the first IFD/page gpstag = tif.pages[0].tags['GPSTag'] # seek to the beginning of the GPSIFD tif.filehandle.seek(gpstag.valueoffset) # load the IFD gpsifd = tifffile.TiffPage(tif, None) # patch the GPSAltitude (6) field gpsifd.tags[6].overwrite(tif, (140, 1)) 

4 Comments

Doesn't seem to work. The error is TypeError: unexpected keyword argument: mode Seems like there is no "mode" parameter. Could you please modify your answer?
You are likely using an outdated version of tifffile. There is a mode parameter
You are right. pip install got me an older version because I used Python 3.6. It seems like from v2020.10.1 Tifffile requires python >= 3.7
Do you know how could I update the existing file using python2.7?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.