Skip to content

Instantly share code, notes, and snippets.

@sanvarie
Last active Feb 9, 2016
Embed
What would you like to do?
画像にジオタグを追加するスクリプトです。
# -*- coding: utf-8 -*-
import pyexiv2
from PIL import Image
def to_deg(value, loc):
if value < 0:
loc_value = loc[0]
elif value > 0:
loc_value = loc[1]
else:
loc_value = ""
abs_value = abs(value)
deg = int(abs_value)
t1 = (abs_value-deg)*60
min = int(t1)
sec = round((t1 - min)* 60, 5)
return (deg, min, sec, loc_value)
def set_gps_location(file_name, lat, lng):
lat_deg = to_deg(lat, ["S", "N"])
lng_deg = to_deg(lng, ["W", "E"])
# 緯度、経度を10進法→60進法(度分秒)に変換
exiv_lat = (pyexiv2.Rational(lat_deg[0]*60+lat_deg[1],60),pyexiv2.Rational(lat_deg[2]*100,6000), pyexiv2.Rational(0, 1))
exiv_lng = (pyexiv2.Rational(lng_deg[0]*60+lng_deg[1],60),pyexiv2.Rational(lng_deg[2]*100,6000), pyexiv2.Rational(0, 1))
metadata = pyexiv2.ImageMetadata(file_name)
metadata.read()
#メタデータを付与
metadata["Exif.GPSInfo.GPSLatitude"] = exiv_lat
metadata["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3]
metadata["Exif.GPSInfo.GPSLongitude"] = exiv_lng
metadata["Exif.GPSInfo.GPSLongitudeRef"] = lng_deg[3]
metadata["Exif.Image.GPSTag"] = 654
metadata["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
metadata["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'
#メタデータを上書き
metadata.write()
#ファイル名、緯度、経度を指定
set_gps_location(r"D:\python\geotag\a020.jpg", 34.6874422,135.1758424)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment