使用 python 对 jpeg 图像进行地理标记的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/453395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:09:24  来源:igfitidea点击:

What is the best way to geotag jpeg-images with python?

pythonjpegexifgeotagging

提问by Frank Bannister

I have coordinates from some source and want to tag my jpg files with them. What is the best python library for writing geotags into exif data?

我有来自某个来源的坐标,并想用它们标记我的 jpg 文件。将地理标签写入 exif 数据的最佳 Python 库是什么?

回答by Simon Willison

I haven't tried it myself, but from the documentation [pyexiv2][1] looks like it should do the job.

我自己还没有尝试过,但从文档 [pyexiv2][1] 看起来它应该可以完成这项工作。

[1]: http://tilloy.net/dev/pyexiv2/tutorial.html#link was missing last character

[1]:http: //tilloy.net/dev/pyexiv2/tutorial.html #link 缺少最后一个字符

回答by Maksym Kozlenko

Here is an example how to set GPS position using pyexiv2 library. I've tested this script by uploading geotagged image to Panoramio

以下是如何使用 pyexiv2 库设置 GPS 位置的示例。我已经通过将带有地理标记的图像上传到 Panoramio 来测试此脚本

#!/usr/bin/env python

import pyexiv2
import fractions
from PIL import Image
from PIL.ExifTags import TAGS
import sys

def to_deg(value, loc):
        if value < 0:
            loc_value = loc[0]
        elif value > 0:
            loc_value = loc[1]
        else:
            loc_value = ""
        abs_value = abs(value)
        deg =  int(abs_value)
        t1 = (abs_value-deg)*60
        min = int(t1)
        sec = round((t1 - min)* 60, 5)
        return (deg, min, sec, loc_value)    

def set_gps_location(file_name, lat, lng):
    """Adds GPS position as EXIF metadata

    Keyword arguments:
    file_name -- image file 
    lat -- latitude (as float)
    lng -- longitude (as float)

    """
    lat_deg = to_deg(lat, ["S", "N"])
    lng_deg = to_deg(lng, ["W", "E"])

    print lat_deg
    print lng_deg

    # convert decimal coordinates into degrees, munutes and seconds
    exiv_lat = (pyexiv2.Rational(lat_deg[0]*60+lat_deg[1],60),pyexiv2.Rational(lat_deg[2]*100,6000), pyexiv2.Rational(0, 1))
    exiv_lng = (pyexiv2.Rational(lng_deg[0]*60+lng_deg[1],60),pyexiv2.Rational(lng_deg[2]*100,6000), pyexiv2.Rational(0, 1))

    exiv_image = pyexiv2.Image(file_name)
    exiv_image.readMetadata()
    exif_keys = exiv_image.exifKeys() 

    exiv_image["Exif.GPSInfo.GPSLatitude"] = exiv_lat
    exiv_image["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3]
    exiv_image["Exif.GPSInfo.GPSLongitude"] = exiv_lng
    exiv_image["Exif.GPSInfo.GPSLongitudeRef"] = lng_deg[3]
    exiv_image["Exif.Image.GPSTag"] = 654
    exiv_image["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
    exiv_image["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'

    exiv_image.writeMetadata()

set_gps_location(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]))

回答by gimel

pexifwas written with geotags as a goal (my emphasis):

pexif是以地理标签为目标编写的(我的重点):

pexif is a Python library for parsing and more importantly editing EXIF data in JPEG files.

This grew out of a need to add GPS tagged data to my images, Unfortunately the other libraries out there couldn't do updates and didn't seem easily architectured to be able to add such a thing. Ain't reusable software grand!

My main reason for writing this was to provide an easy way for geo-tagging my photos, and the library now seems mature enough to do that.

pexif 是一个 Python 库,用于解析和更重要的是编辑 JPEG 文件中的 EXIF 数据。

这源于需要向我的图像添加 GPS 标记数据,不幸的是,其他库无法进行更新,并且似乎不容易构建能够添加这样的东西。是不是可重用软件盛大!

我写这篇文章的主要原因是提供一种简单的方法来对我的照片进行地理标记,现在这个库似乎已经足够成熟了。

回答by Crazy Otto

the above code works, but I had to modify the set_gps_location function to work with the current version of pyexiv2...perhaps Maksym was using an older version:

上面的代码有效,但我必须修改 set_gps_location 函数才能使用当前版本的 pyexiv2 ...也许 Maksym 使用的是旧版本:

def set_gps_location(file_name, lat, lng):
    """Adds GPS position as EXIF metadata

    Keyword arguments:
    file_name -- image file
    lat -- latitude (as float)
    lng -- longitude (as float)

    """
    lat_deg = to_deg(lat, ["S", "N"])
    lng_deg = to_deg(lng, ["W", "E"])

    print lat_deg
    print lng_deg

    # convert decimal coordinates into degrees, munutes and seconds
    exiv_lat = (pyexiv2.Rational(lat_deg[0]*60+lat_deg[1],60),pyexiv2.Rational(lat_deg[2]*100,6000), pyexiv2.Rational(0, 1))
    exiv_lng = (pyexiv2.Rational(lng_deg[0]*60+lng_deg[1],60),pyexiv2.Rational(lng_deg[2]*100,6000), pyexiv2.Rational(0, 1))
    metadata = pyexiv2.ImageMetadata(file_name)
    metadata.read()

##    exif_keys = metadata.exif_keys

    metadata["Exif.GPSInfo.GPSLatitude"] = exiv_lat
    metadata["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3]
    metadata["Exif.GPSInfo.GPSLongitude"] = exiv_lng
    metadata["Exif.GPSInfo.GPSLongitudeRef"] = lng_deg[3]
    metadata["Exif.Image.GPSTag"] = 654
    metadata["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
    metadata["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'

    metadata.write()

回答by heltonitba

pyexiv2 is now deprecated in favour of GExiv2, a GObject-based wrapper around libexiv2.

pyexiv2 现在已被弃用,取而代之的是GExiv2,这是一个围绕 libexiv2 的基于 GObject 的包装器。