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

时间:2022-09-25 21:19:56

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库是什么?

5 个解决方案

#1


pexif was 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.

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

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!

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

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.

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

#2


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缺少最后一个字符

#3


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]))

#4


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()

#5


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

pyexiv2现在已被弃用,以支持GExiv2,这是一个围绕libexiv2的基于GObject的包装器。

#1


pexif was 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.

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

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!

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

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.

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

#2


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缺少最后一个字符

#3


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]))

#4


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()

#5


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

pyexiv2现在已被弃用,以支持GExiv2,这是一个围绕libexiv2的基于GObject的包装器。