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

2024-05-18 10:52:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些来源的坐标,想用它们标记我的jpg文件。将geotag写入exif数据的最佳python库是什么?


Tags: 文件数据标记来源exifjpggeotag
3条回答

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

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

下面是一个如何使用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]))

pexif是以geotags为目标编写的(我的重点是):

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.

相关问题 更多 >