提取文件夹中的jpg文件中的pos信息
时间: 2024-02-19 07:02:48 浏览: 19
您可以使用Python中的Pillow库来读取jpg图片的exif信息,其中包括了图片的拍摄位置(如果有的话)。以下是一个示例代码:
```python
from PIL import Image
from PIL.ExifTags import TAGS
def get_exif_data(image_path):
image = Image.open(image_path)
exif_data = image._getexif()
if exif_data:
exif_data_dict = {}
for tag_id, value in exif_data.items():
tag = TAGS.get(tag_id, tag_id)
exif_data_dict[tag] = value
return exif_data_dict
else:
return None
def get_image_pos(image_path):
exif_data = get_exif_data(image_path)
if exif_data and 'GPSInfo' in exif_data:
lat_ref = exif_data['GPSInfo'][1]
lat = exif_data['GPSInfo'][2]
lon_ref = exif_data['GPSInfo'][3]
lon = exif_data['GPSInfo'][4]
lat = (lat[0][0]/lat[0][1]) + (lat[1][0]/lat[1][1])/60 + (lat[2][0]/lat[2][1])/3600
lon = (lon[0][0]/lon[0][1]) + (lon[1][0]/lon[1][1])/60 + (lon[2][0]/lon[2][1])/3600
if lat_ref == 'S':
lat = -lat
if lon_ref == 'W':
lon = -lon
return lat, lon
else:
return None
folder_path = "/path/to/folder"
for file_name in os.listdir(folder_path):
if file_name.endswith(".jpg"):
file_path = os.path.join(folder_path, file_name)
pos = get_image_pos(file_path)
if pos:
print(f"{file_name}: {pos}")
```
这段代码会遍历指定文件夹中的所有jpg文件,并提取其中的拍摄位置信息。如果没有位置信息,则不会输出任何内容。请注意,这段代码仅适用于包含GPS信息的jpg文件,如果没有GPS信息,则无法提取位置信息。