레이블이 ADDRESS인 게시물을 표시합니다. 모든 게시물 표시
레이블이 ADDRESS인 게시물을 표시합니다. 모든 게시물 표시

2023년 2월 10일 금요일

JPEG 이미지에서 GPS 위치 정보 추출 후, 주소로 전환

JPEG 이미지에서 GPS 위치 정보 추출 후, 주소로 전환.

이미지가 획득된 시간 정보도 출력. 


# From Image through GPS location to Address
from geopy.geocoders import Nominatim
from exif import Image as exImg


def geocoding_reverse(lat_lng_str):
    geolocoder = Nominatim(user_agent = 'South Korea', timeout=None)
    address = geolocoder.reverse(lat_lng_str)
    return address

def decimal_coords(coords, ref):
    decimal_degrees = coords[0] + coords[1] / 60 + coords[2] / 3600
    if ref == "S" or ref =='W' :
        decimal_degrees = -decimal_degrees
    return decimal_degrees

def image_coordinates(img):
    if img.has_exif:
        try:
            coords = (decimal_coords(img.gps_latitude, img.gps_latitude_ref),
                         decimal_coords(img.gps_longitude, img.gps_longitude_ref))
        except AttributeError:
            print ('No Coordinates')
            return {}
    else:
        print ('The Image has no EXIF information')
        return {}
    return {"imageTakenTime":img.datetime_original, "geolocation_lat":coords[0],"geolocation_lng":coords[1]}


image_path = 'bbb.jpg"
with open(image_path, 'rb') as src:
    img = exImg(src)

img_info = image_coordinates(img)
if "imageTakenTime" in img_info:
    posi = '{},{}'.format(img_info["geolocation_lat"],img_info["geolocation_lng"])
    address = geocoding_reverse(posi)
    print(img_info["imageTakenTime"], address)

#address = geocoding_reverse('36.5760732781656, 128.15935928504484')
#print(address)