简体   繁体   English

使用 Python 3.6 覆盖 Image Exif 中的 GPS 坐标

[英]Overwrite GPS coordinates in Image Exif using Python 3.6

I am trying to transform image geotags so that images and ground control points lie in the same coordinate system inside my software (Pix4D mapper).我正在尝试转换图像地理标记,以便图像和地面控制点位于我的软件(Pix4D 映射器)内的同一坐标系中。

The answer here says: 这里的答案说:

Exif data is standardized, and GPS data must be encoded using geographical coordinates (minutes, seconds, etc) described above instead of a fraction. Exif 数据是标准化的,GPS 数据必须使用上述地理坐标(分钟、秒等)而不是分数进行编码。 Unless it's encoded in that format in the exif tag, it won't stick.除非它在 ​​exif 标签中以该格式编码,否则它不会粘住。

Here is my code:这是我的代码:

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)

I am getting struct.error: argument out of range in the dump method.我在转储方法中收到 struct.error: argument out of range 。 The original GPSInfo tag looks like: {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)}原始 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)}

I am guessing I have to offset the values and encode them properly before writing, but have no idea what is to be done.我猜我必须在写入之前偏移值并正确编码它们,但不知道要做什么。

It looks like you are already using PIL and Python 3.x, not sure if you want to continue using piexif but either way, you may find it easier to convert the degrees, minutes, and seconds into decimal first.看起来您已经在使用 PIL 和 Python 3.x,不确定是否要继续使用piexif但无论哪种方式,您可能会发现piexif度、分和秒转换为十进制更容易。 It looks like you are trying to do that already but putting it in a separate function may be clearer and account for direction reference.看起来您已经在尝试这样做,但将其放在单独的函数中可能会更清晰并考虑方向参考。

Here's an example:下面是一个例子:

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)

The geotags in this example is a dictionary with the GPSTAGS as keys instead of the numeric codes for readability.本例中的geotags是一个字典,以 GPSTAGS 作为关键字,而不是数字代码,以提高可读性。 You can find more detail and the complete example from this blog post: Getting Started with Geocoding Exif Image Metadata in Python 3您可以在此博客文章中找到更多详细信息和完整示例: Python 3 中的地理编码 Exif 图像元数据入门

After much hemming & hawing I reached the pages of py3exiv2 image metadata manipulation library.经过大量的折边和折腾后,我到达了py3exiv2图像元数据操作库的页面。 One will find exhaustive lists of the metadata tags as one reads through but here is the list of EXIF tags just to save few clicks.人们会在阅读时找到详尽的元数据标签列表,但这里是EXIF 标签列表,只是为了节省几次点击。

It runs smoothly on Linux and provides many opportunities to edit image-headers.在 Linux 上运行流畅,并提供了许多编辑图像标题的机会。 The documentation is also quite clear.文档也很清楚。 I recommend this as a solution and am interested to know if it solves everyone else's problems as well.我建议将此作为解决方案,并且很想知道它是否也能解决其他所有人的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM