用智能手机或数码相机拍摄的照片有丰富的附加信息元数据,通常以 EXIF 格式存储这些元数据,EXIF 即Exchangeable Image File Format,有不同的标准版本。 EXIF 元数据可能包含关于设备模型、尺寸、图片的日期及其位置的信息。
安装exif 库
pip install exif
读取图片
from exif import Image
img_path = 'images/image_exif.jpg'
with open(img_path, 'rb') as src:
img = Image(src)
print (src.name, img)
输出内容为:
images/image_exif.jpg <exif._image.Image object at 0x00000223EBC62430>
通过PTL库读取该输出即可获取图像内容:
import PIL
image = PIL.Image.open(img_path)
image
检查图片中是否含有EXIF数据:
if img.has_exif:
info = f"has the EXIF {img.exif_version}"
else:
info = "does not contain any EXIF information"
print(f"Image {src.name}: {info}")
若有,提示:
Image images/image_exif.jpg: has the EXIF 0220
没有:
with open('images/foto_no_exif.jpg', "rb") as src:
img = Image(src)
if img.has_exif:
info = f" has the EXIF {img.exif_version}"
else:
info = "does not contain any EXIF information"
print(f"Image {src.name}: {info}")
输出:
Image images/foto_no_exif.jpg: does not contain any EXIF information
获取图像中的EXIF元数据
with open(img_path, “rb”) as src:
img = Image(src)
img.list_all()
该列表显示了元数据中所有的标签内容:
['image_description',
'make',
'model',
'orientation',
'x_resolution',
'y_resolution',
'resolution_unit',
'software',
'datetime',
'y_and_c_positioning',
'_exif_ifd_pointer',
'_gps_ifd_pointer',
'compression',
'jpeg_interchange_format',
'jpeg_interchange_format_length',
'exposure_time',
'f_number',
'exposure_program',
'photographic_sensitivity',
'exif_version',
'datetime_original',
'datetime_digitized',
'components_configuration',
'exposure_bias_value',
'max_aperture_value',
'metering_mode',
'light_source',
'flash',
'focal_length',
'maker_note',
'user_comment',
'flashpix_version',
'color_space',
'pixel_x_dimension',
'pixel_y_dimension',
'_interoperability_ifd_Pointer',
'file_source',
'scene_type',
'custom_rendered',
'exposure_mode',
'white_balance',
'digital_zoom_ratio',
'focal_length_in_35mm_film',
'scene_capture_type',
'gain_control',
'contrast',
'saturation',
'sharpness',
'subject_distance_range',
'gps_latitude_ref',
'gps_latitude',
'gps_longitude_ref',
'gps_longitude',
'gps_altitude_ref',
'gps_timestamp',
'gps_satellites',
'gps_img_direction_ref',
'gps_map_datum',
'gps_datestamp']
获取图像中的地理坐标
将经纬度坐标转换为十进制数值表示:
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_path):
with open(img_path, 'rb') as src:
img = Image(src)
if img.has_exif:
try:
img.gps_longitude
coords = (decimal_coords(img.gps_latitude,
img.gps_latitude_ref),
decimal_coords(img.gps_longitude,
img.gps_longitude_ref))
except AttributeError:
print 'No Coordinates'
else:
print 'The Image has no EXIF information'
print(f"Image {src.name}, OS Version:{img.get('software', 'Not Known')} ------")
print(f"Was taken: {img.datetime_original}, and has coordinates:{coords}")
输出内容:
Image images/DSCN0012.jpg, OS Version:Nikon Transfer 1.1 W ------
Was taken: 2008:10:22 16:29:49, and has coordinates:(43.46715666666389, 11.885394999997223)
参考:
https://medium.com/spatial-data-science/how-to-extract-gps-coordinates-from-images-in-python-e66e542af354
|