diff --git a/tools/convert_jpg.py b/tools/convert_jpg.py index fd9bf97..cefdcbe 100644 --- a/tools/convert_jpg.py +++ b/tools/convert_jpg.py @@ -13,9 +13,9 @@ def convert_image(file_name, img_dir, output_dir, convert_format): img.save(output_path) def main(): - convert_format = "tif" + convert_format = "jpg" img_dir = r"E:\datasets\UAV\134\project\images" - output_dir = r"E:\datasets\UAV\134_tif\project\images" + output_dir = r"E:\datasets\UAV\134_jpg\project\images" # 如果输出目录存在,先删除 if os.path.exists(output_dir): diff --git a/tools/read_gps.py b/tools/read_gps.py new file mode 100644 index 0000000..47166bf --- /dev/null +++ b/tools/read_gps.py @@ -0,0 +1,63 @@ +import os +import piexif +from PIL import Image + +def dms_to_decimal(dms): + """将DMS格式转换为十进制度""" + if not dms: + return None + degrees = dms[0][0] / dms[0][1] + minutes = dms[1][0] / dms[1][1] / 60 + seconds = dms[2][0] / dms[2][1] / 3600 + return degrees + minutes + seconds + +def get_gps_info(image_path): + """获取图片的GPS信息""" + try: + image = Image.open(image_path) + exif_data = piexif.load(image.info['exif']) + + gps_info = exif_data.get("GPS", {}) + if not gps_info: + return None, None, None + + # 获取纬度 + lat = dms_to_decimal(gps_info.get(2)) + if lat and gps_info.get(1) and gps_info[1] == b'S': + lat = -lat + + # 获取经度 + lon = dms_to_decimal(gps_info.get(4)) + if lon and gps_info.get(3) and gps_info[3] == b'W': + lon = -lon + + # 获取高度 + alt = None + if 6 in gps_info: + alt = gps_info[6][0] / gps_info[6][1] + + return lat, lon, alt + + except Exception as e: + print(f"读取文件 {image_path} 时出错: {str(e)}") + return None, None, None + +def main(): + # 设置输入输出路径 + image_dir = r"E:\datasets\UAV\134\project\images" + output_path = r"E:\datasets\UAV\134\project\gps.txt" + + with open(output_path, 'w', encoding='utf-8') as f: + for filename in os.listdir(image_dir): + if filename.lower().endswith(('.jpg', '.jpeg')): + image_path = os.path.join(image_dir, filename) + lat, lon, alt = get_gps_info(image_path) + + if lat is not None and lon is not None: + # 如果没有高度信息,使用0 + alt = alt if alt is not None else 0 + filename = filename.replace(".jpg", ".tif") + f.write(f"{filename} {lat} {lon} {alt}\n") + +if __name__ == '__main__': + main()