我正在尝试转换图像地理标记,以便图像和地面控制点位于我的软件(Pix4D 映射器)内的同一坐标系中。
答案here说:
Exif data is standardized, and GPS data must be encoded using geographical coordinates (minutes, seconds, etc) described above instead of a fraction. Unless it's encoded in that format in the exif tag, it won't stick.
这是我的代码:
import os, piexif, pyproj
from PIL import Image
img = Image.open(os.path.join(dirPath,fn))
exif_dict = piexif.load(img.info['exif'])
breite = exif_dict['GPS'][piexif.GPSIFD.GPSLatitude]
lange = exif_dict['GPS'][piexif.GPSIFD.GPSLongitude]
breite = breite[0][0] / breite[0][1] + breite[1][0] / (breite[1][1] * 60) + breite[2][0] / (breite[2][1] * 3600)
lange = lange[0][0] / lange[0][1] + lange[1][0] / (lange[1][1] * 60) + lange[2][0] / (lange[2][1] * 3600)
print(breite) #48.81368778730952
print(lange) #9.954511162420633
x, y = pyproj.transform(wgs84, gk3, lange, breite) #from WGS84 to GaussKrüger zone 3
print(x) #3570178.732528623
print(y) #5408908.20172699
exif_dict['GPS'][piexif.GPSIFD.GPSLatitude] = [ ( (int)(round(y,6) * 1000000), 1000000 ), (0, 1), (0, 1) ]
exif_bytes = piexif.dump(exif_dict) #error here
img.save(os.path.join(outPath,fn), "jpeg", exif=exif_bytes)
我收到 struct.error: dump 方法中的参数超出范围。原始 GPSInfo 标记如下所示: {0: b'\x02\x03\x00\x00', 1: 'N', 2: ((48, 1), (48, 1), (3449322402, 70000000 )), 3: 'E', 4: ((9, 1), (57, 1), (1136812930, 70000000)), 5: b'\x00', 6: (3659, 10)}
我猜我必须在写入之前偏移这些值并对其进行正确编码,但不知道要做什么。
最佳答案
看起来您已经在使用 PIL 和 Python 3.x,不确定是否要继续使用 piexif
但无论哪种方式,您可能会发现转换度数、分钟数和秒先转为小数。看起来您已经在尝试这样做,但将其放在单独的函数中可能会更清晰并说明方向引用。
这是一个例子:
def get_decimal_from_dms(dms, ref):
degrees = dms[0][0] / dms[0][1]
minutes = dms[1][0] / dms[1][1] / 60.0
seconds = dms[2][0] / dms[2][1] / 3600.0
if ref in ['S', 'W']:
degrees = -degrees
minutes = -minutes
seconds = -seconds
return round(degrees + minutes + seconds, 5)
def get_coordinates(geotags):
lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef'])
lon = get_decimal_from_dms(geotags['GPSLongitude'], geotags['GPSLongitudeRef'])
return (lat,lon)
此示例中的geotags
是一个字典,以 GPSTAGS 作为键而不是数字代码以提高可读性。您可以从此博文中找到更多详细信息和完整示例:Getting Started with Geocoding Exif Image Metadata in Python 3
关于python-3.x - 使用Python 3.6覆盖图像Exif中的GPS坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54196347/