fur*_*ohn 5 python jpeg geotagging pyexiv2
我正在使用pyexiv2 Python模块使用另一个SO答案中找到的代码对JPEG进行地理标记(请参阅:使用Python对jpeg-images进行地理标记的最佳方法是什么?)我对GPSTag值有疑问.
答案中给出的代码如下:
exiv_image["Exif.Image.GPSTag"] = 654
exiv_image["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
exiv_image["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'
Run Code Online (Sandbox Code Playgroud)
我查看了Exiv2文档并找到了GPSTag,GPSMapDatum和GPSVersionID的描述,但我仍然对GPSTag的价值感到困惑.
从文档中可以看出:
指向GPS Info IFD的指针.GPS Info IFD的互操作性结构与Exif IFD的互操作性结构一样,没有图像数据.
这个描述并没有真正解释如何确定使用什么值,我无法在线找到更好的GPSTag描述.
所以我的问题是:
谢谢你的帮助.
如果您想从 pyexiv2 访问 GPS 数据,该代码如下所示:
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
Run Code Online (Sandbox Code Playgroud)
写作看起来像这样:
self.exif[GPS + 'AltitudeRef'] = '0' if self.altitude >= 0 else '1'
self.exif[GPS + 'Altitude'] = Fraction(self.altitude)
self.exif[GPS + 'Latitude'] = decimal_to_dms(self.latitude)
self.exif[GPS + 'LatitudeRef'] = 'N' if self.latitude >= 0 else 'S'
self.exif[GPS + 'Longitude'] = decimal_to_dms(self.longitude)
self.exif[GPS + 'LongitudeRef'] = 'E' if self.longitude >= 0 else 'W'
self.exif[GPS + 'MapDatum'] = 'WGS-84'
Run Code Online (Sandbox Code Playgroud)
有了这些支持功能:
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)]
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
* pyexiv2's author, Olivier Tilloy, has asked me for help with maintainership as he no longer has much time
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3219 次 |
| 最近记录: |