<p>使用pyexv2对照片进行地理标记的最佳方法是使用<a href="http://github.com/robru/gottengeography" rel="nofollow">my program, GottenGeography</a>;-)</p>
<p>但说真的,如果你想从PyExv2访问GPS数据,代码如下所示:</p>
<pre><code> GPS = 'Exif.GPSInfo.GPS'
try:
self.latitude = dms_to_decimal(
*self.exif[GPS + 'Latitude'].value +
[self.exif[GPS + 'LatitudeRef'].value]
)
self.longitude = dms_to_decimal(
*self.exif[GPS + 'Longitude'].value +
[self.exif[GPS + 'LongitudeRef'].value]
)
except KeyError:
pass
try:
self.altitude = float(self.exif[GPS + 'Altitude'].value)
if int(self.exif[GPS + 'AltitudeRef'].value) > 0:
self.altitude *= -1
except KeyError:
pass
</code></pre>
<p>写作看起来是这样的:</p>
^{pr2}$
<p>有了这些支持功能:</p>
<pre><code>class Fraction(fractions.Fraction):
"""Only create Fractions from floats.
>>> Fraction(0.3)
Fraction(3, 10)
>>> Fraction(1.1)
Fraction(11, 10)
"""
def __new__(cls, value, ignore=None):
"""Should be compatible with Python 2.6, though untested."""
return fractions.Fraction.from_float(value).limit_denominator(99999)
def dms_to_decimal(degrees, minutes, seconds, sign=' '):
"""Convert degrees, minutes, seconds into decimal degrees.
>>> dms_to_decimal(10, 10, 10)
10.169444444444444
>>> dms_to_decimal(8, 9, 10, 'S')
-8.152777777777779
"""
return (-1 if sign[0] in 'SWsw' else 1) * (
float(degrees) +
float(minutes) / 60 +
float(seconds) / 3600
)
def decimal_to_dms(decimal):
"""Convert decimal degrees into degrees, minutes, seconds.
>>> decimal_to_dms(50.445891)
[Fraction(50, 1), Fraction(26, 1), Fraction(113019, 2500)]
>>> decimal_to_dms(-125.976893)
[Fraction(125, 1), Fraction(58, 1), Fraction(92037, 2500)]
"""
remainder, degrees = math.modf(abs(decimal))
remainder, minutes = math.modf(remainder * 60)
return [Fraction(n) for n in (degrees, minutes, remainder * 60)]
</code></pre>
<p>尽管我目前正在研究一个pyexiv2的替代品,它使用GObject自省来更直接地访问exiv2库,称为<a href="http://github.com/robru/gexiv2" rel="nofollow">GExiv2</a>,我希望能对它有一些反馈。gexiv2和pyexiv2都是同一个exiv2库的包装器,但不同之处在于pyexiv2是一个非常大的项目,有很多胶水,只能在python中工作,并且处于被放弃的边缘*;而gexiv2轻巧灵活,可以从任何编程语言访问,并且由于Shotwell的使用而得到了很好的维护。在</p>
<p>希望这有帮助!在</p>
<pre><code>* pyexiv2's author, Olivier Tilloy, has asked me for help with maintainership as he no longer has much time
</code></pre>