29

I am trying to edit/modify existing metadata within python 2.7. More specifically I have GPS coordinates in a my metedata, however the altitude field is incorrect. Is there a way of changing this?

I have had a look at PIL piexif pyexif, but I cannot seem to find a way to modify existing fields.

Has anyone managed to do this? It sounds like it would be very simple, but I can't seem to work it out.

demonplus
  • 5,613
  • 12
  • 49
  • 68
D.Griffiths
  • 2,248
  • 3
  • 16
  • 30

2 Answers2

32
import piexif
from PIL import Image

img = Image.open(fname)
exif_dict = piexif.load(img.info['exif'])

altitude = exif_dict['GPS'][piexif.GPSIFD.GPSAltitude]
print(altitude)

(550, 1) % some values are saved in a fractional format. This means 550m, (51, 2) would be 25,5m.

exif_dict['GPS'][piexif.GPSIFD.GPSAltitude] = (140, 1)

This sets the altitude to 140m

exif_bytes = piexif.dump(exif_dict)
img.save('_%s' % fname, "jpeg", exif=exif_bytes)
Franz Forstmayr
  • 1,219
  • 1
  • 15
  • 31
  • 13
    It would be worth stating that using this approach (in particular using the **save** function) might result in a modification of the image content. If one wants to keep the content of the image as-is, then it is better using this other approach : [link](https://stackoverflow.com/questions/53543549/change-exif-data-on-jpeg-without-altering-picture) – abletterer Dec 19 '18 at 14:25
4

Late answer, but you can use GPSPhoto, i.e.:

from GPSPhoto import gpsphoto
photo = gpsphoto.GPSPhoto("photo.jpg")

# Create GPSInfo Data Object
# info = gpsphoto.GPSInfo((38.71615498471598, -9.148730635643007))
# info = gpsphoto.GPSInfo((38.71615498471598, -9.148730635643007), timeStamp='2018:12:25 01:59:05')'''
info = gpsphoto.GPSInfo((38.71615498471598, -9.148730635643007), alt=83, timeStamp='2018:12:25 01:59:05')

# Modify GPS Data
photo.modGPSData(info, 'new_photo.jpg')

Installation:

pip install GPSPhoto
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 3
    Beware, this modifies the image data. If you want to keep your image data untouched, better use piexif + @abletterer's comment – user1256821 Jan 27 '19 at 15:17