How can I write full gps coordinates in degrees, minutes, seconds format as a GPS exif tag to .tif images using python? I can write a portion of the gps coord using the piexif package (e.g. Longitude) if I only include the decimal and minutes as integers. However piexif throws a ValueError whenever I include seconds and fractions of a second. piexif will only accept a tuple containing two integers for the longitude, despite the standard calling for 3 integers.
Info on EXIF tags is available [here](http://www.cipa.jp/std/documents/e/DC- 008-2012_E.pdf)!
import piexif # pip install piexif
from PIL import Image # PIL version 4.0.0 for compatability with conda/py3.6
fname = "foo.tiff"
#read tiff file into a pillow image obj
im = Image.open(fname)
#readin any existing exif data to a dict
exif_dict = piexif.load(fname)
#GPS coord to write to GPS tag in dms format
#long = 120° 37' 42.9996" East longitude
LongRef = "E"
Long_d = 120
Long_m = 37
Long_s = 42.9996
#add gps data to EXIF containing GPS dict
#exif_dict['GPS'][piexif.GPSIFD.GPSLongitude] = (Long_d, Long_m) #this works
exif_dict['GPS'][piexif.GPSIFD.GPSLongitude] = (Long_d, (Long_m,Long_s)) #this returns an error
exif_dict['GPS'][piexif.GPSIFD.GPSLongitude] = (Long_d, Long_m, Long_s) #this also returns an error
"""
Traceback (most recent call last):
File "<ipython-input-210-918cd4e2989f>", line 7, in <module>
exif_bytes = piexif.dump(exif_dict)
File "C:\Users\JF\AppData\Local\Continuum\anaconda3\lib\site-packages\piexif-1.0.13-py3.6.egg\piexif\_dump.py", line 74, in dump
gps_set = _dict_to_bytes(gps_ifd, "GPS", zeroth_length + exif_length)
File "C:\Users\JF\AppData\Local\Continuum\anaconda3\lib\site-packages\piexif-1.0.13-py3.6.egg\piexif\_dump.py", line 341, in _dict_to_bytes
'{0} in {1} IFD. Got as {2}.'.format(key, ifd, type(ifd_dict[key]))
ValueError: "dump" got wrong type of exif value.
4 in GPS IFD. Got as <class 'tuple'>.
"""
#convert updated GPS dict to exif_bytes
exif_bytes = piexif.dump(exif_dict)
#encode updated exif tag into image and save as a jpeg
im.save(fname.replace('.tiff','.jpeg'), "jpeg", exif=exif_bytes)
我没有使用piexif,但是根据他们的文档,您需要指定
rational
格式的数据,如(int, int)
:基本上用
(numerator, denominator)
来描述你的值。在相关问题 更多 >
编程相关推荐