使用pyexiv2对JPEG图片进行地理标记

2024-04-28 22:04:59 发布

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

我使用pyexiv2 Python模块,使用在另一个SO答案中找到的代码(请参见:What is the best way to geotag jpeg-images using Python?),我有一个关于GPSTag值的问题。在

答案中给出的代码有以下几行:

exiv_image["Exif.Image.GPSTag"] = 654
exiv_image["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
exiv_image["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'

我查看了Exiv2 documentation,找到了GPSTag、GPSMapDatum和GPSVersionID的描述,但仍然对GPSTag的值感到困惑。在

从文件中可以看出:

A pointer to the GPS Info IFD. The Interoperability structure of the GPS Info IFD, like that of Exif IFD, has no image data.

这个描述并没有真正解释如何确定使用什么值,我也没能在网上找到更好的GPSTag描述。在

所以我的问题是:

  1. 给定一个新图像,如何确定Exif.Image.GPSTag?在
  2. 为什么代码示例使用的值是654(这可以通过问题1来回答)?在

谢谢你的帮助。在


Tags: theto答案代码imageinfogpsexif
2条回答

使用pyexv2对照片进行地理标记的最佳方法是使用my program, GottenGeography;-)

但说真的,如果你想从PyExv2访问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

写作看起来是这样的:

^{pr2}$

有了这些支持功能:

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

尽管我目前正在研究一个pyexiv2的替代品,它使用GObject自省来更直接地访问exiv2库,称为GExiv2,我希望能对它有一些反馈。gexiv2和pyexiv2都是同一个exiv2库的包装器,但不同之处在于pyexiv2是一个非常大的项目,有很多胶水,只能在python中工作,并且处于被放弃的边缘*;而gexiv2轻巧灵活,可以从任何编程语言访问,并且由于Shotwell的使用而得到了很好的维护。在

希望这有帮助!在

* pyexiv2's author, Olivier Tilloy, has asked me for help with maintainership as he no longer has much time

我的版本,有点长。。。在

from fractions import Fraction
import pyexiv2

try:
    metadata = pyexiv2.metadata.ImageMetadata(image_file)
    metadata.read();
    thumb = metadata.exif_thumbnail

    try:
        latitude = metadata.__getitem__("Exif.GPSInfo.GPSLatitude")
        latitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLatitudeRef")
        longitude = metadata.__getitem__("Exif.GPSInfo.GPSLongitude")
        longitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLongitudeRef")

        latitude = str(latitude).split("=")[1][1:-1].split(" ");
        latitude = map(lambda f: str(float(Fraction(f))), latitude)
        latitude = latitude[0] + u"\u00b0" + latitude[1] + "'" + latitude[2] + '"' + " " + str(latitudeRef).split("=")[1][1:-1]

        longitude = str(longitude).split("=")[1][1:-1].split(" ");
        longitude = map(lambda f: str(float(Fraction(f))), longitude)
        longitude = longitude[0] + u"\u00b0" + longitude[1] + "'" + longitude[2] + '"' + " " + str(longitudeRef).split("=")[1][1:-1]

        latitude_value = dms_to_decimal(*metadata.__getitem__("Exif.GPSInfo.GPSLatitude").value + [metadata.__getitem__("Exif.GPSInfo.GPSLatitudeRef").value]);
        longitude_value = dms_to_decimal(*metadata.__getitem__("Exif.GPSInfo.GPSLongitude").value + [metadata.__getitem__("Exif.GPSInfo.GPSLongitudeRef").value]);

        print " - GPS  -"
        print "Coordinates: " + latitude + ", " + longitude
        print "Coordinates: " + str(latitude_value) + ", " + str(longitude_value)
        print " - GPS  -"
    except Exception, e:
        print "No GPS Information!"
        #print e

    # Check for thumbnail
    if(thumb.data == ""):
        print "No thumbnail!"
except Exception, e:
    print "Error processing image..."
    print e;

相关问题 更多 >