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

空扰寡人 提交于 2019-12-04 13:21:59

问题


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?


回答1:


pexif was written with geotags as a goal (my emphasis):

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.




回答2:


Here is an example how to set GPS position using pyexiv2 library. I've tested this script by uploading geotagged image to 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]))



回答3:


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

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




回答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:

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.



来源:https://stackoverflow.com/questions/453395/what-is-the-best-way-to-geotag-jpeg-images-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!