2012-05-29 4 views
5

pyexiv2 Python 모듈을 사용하여 다른 SO 답변 (참조 : What is the best way to geotag jpeg-images using Python? 참조)에서 찾은 코드를 사용하여 JPEG에 위치 정보 태그를 지정하고 GPS 태그 값에 대한 질문이 있습니다. 나는 Exiv2 documentation 쳐다 보면서 GPSTag, GPSMapDatum 및 GPSVersionID의 설명을 찾았지만 여전히 GPSTag의 값에 대한 혼란 스러워요 한pyexiv2로 JPEG에 위치 정보 태그 지정하기

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

:

이 질문에 주어진 코드는 다음과 같은 라인을 가지고있다. 그것이 말하는 문서에서

:

는 GPS 정보 IFD에 대한 포인터. Exif IFD와 마찬가지로 GPS Info IFD의 상호 운용성 구조에는 이미지 데이터가 없습니다.

이 설명은 실제로 사용할 값을 결정하는 방법을 설명하지 않으며 GPSTag 온라인에 대한 더 나은 설명을 찾을 수 없었습니다.

그래서 내 질문은 : 새로운 이미지 감안할 때

  1. 는 어떻게 Exif.Image.GPSTag의 가치를 결정 하는가?
  2. 코드 샘플의 값이 654 인 이유는 무엇입니까?

도움 주셔서 감사합니다. 당신이 pyexiv2에서 GPS 데이터에 액세스하고자하는 경우 pyexiv2를 사용하여 사진에 위치 정보 태그를 지정하는

답변

3

가장 좋은 방법은,하지만 심각하게 my program, GottenGeography ;-)

그러나와 확실히, 그 코드는 다음과 같습니다

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 

그리고 쓰기는 다음과 같습니다 이러한 지원 기능으로

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' 

:

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

현재 GIF 인트로 스펙 션을 사용하여 exiv2 라이브러리에 훨씬 더 직접적으로 액세스하는 pyexiv2 대신 GExiv2이라는 대체물을 사용하고 있지만 이에 대한 의견이 있기를 바랍니다. gexiv2와 pyexiv2는 모두 같은 exiv2 라이브러리를 감싸는 래퍼입니다. 그러나 차이점은 pyexiv2는 많은 양의 접착제가 포함 된 매우 큰 프로젝트이며 파이썬에서만 작동하며 포기할 위기에 처해 있다는 것입니다. gexiv2는 가볍고 민첩하며 모든 프로그래밍 언어에서 액세스 할 수 있으며 Shotwell의 사용 덕분에 잘 유지됩니다.

희망이 도움이됩니다.

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

내 버전, 조금 긴 ...

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;