1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでJPEGのEXIFから緯度経度を抽出

Last updated at Posted at 2025-09-21

指定されたフォルダ内のJpegファイルを読み取り、EXIFから緯度経度の情報を抽出してCSVファイルに出力します。

コード

extract_gps_info.py
import os
import csv
import exifread
import argparse

def get_decimal_from_dms(dms, ref):
    values = dms.values  # IfdTagから値リストを取得
    degrees = float(values[0].num) / float(values[0].den)
    minutes = float(values[1].num) / float(values[1].den)
    seconds = float(values[2].num) / float(values[2].den)

    decimal = degrees + (minutes / 60.0) + (seconds / 3600.0)
    if ref in ['S', 'W']:
        decimal = -decimal
    return decimal

def extract_gps_info(jpeg_path):
    with open(jpeg_path, 'rb') as f:
        tags = exifread.process_file(f, details=False)
        try:
            lat = get_decimal_from_dms(
                tags['GPS GPSLatitude'], 
                tags['GPS GPSLatitudeRef'].printable
            )
            lon = get_decimal_from_dms(
                tags['GPS GPSLongitude'], 
                tags['GPS GPSLongitudeRef'].printable
            )
            return lat, lon
        except KeyError:
            return None, None

def main(folder_path, output_csv):
    files = [f for f in os.listdir(folder_path) if f.lower().endswith('.jpg') or f.lower().endswith('.jpeg')]
    with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['filename', 'latitude', 'longitude'])
        for file in files:
            path = os.path.join(folder_path, file)
            lat, lon = extract_gps_info(path)
            writer.writerow([file, lat, lon])

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='JPEGファイルからGPS情報を抽出してCSV出力します。')
    parser.add_argument('folder', help='JPEGファイルが置いてあるフォルダのパス')
    parser.add_argument('output', help='出力するCSVファイル名(例: output.csv)')
    args = parser.parse_args()
    main(args.folder, args.output)

動作確認した環境

Windows 11、Python 3.13.7で動作確認済です。

使い方

必要なパッケージのインストール

Pillow(Python Imaging Library)exifreadが必要です。

pip install Pillow
pip install exifread

コマンドライン

python extract_gps_info.py パス 出力先CSVファイル名

JPEGファイルについての注意

撮影時、デジカメ側でのGPS(GNSS)の情報を記録するよう設定されている必要があります。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?