日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

如何利用python讀取圖片屬性信息_python

作者:alex1801 ? 更新時間: 2022-05-09 編程語言

? ? ? ? 從照片里面獲取GPS信息。可交換圖像文件常被簡稱為EXIF(Exchangeable image file format),是專門為數碼相機的照片設定的,可以記錄數碼照片的屬性信息和拍攝數據,EXIF信息不支持png,webp等圖片格式。

? ? ? ? Python中使用ExifRead包讀取圖片的屬性信息,安裝方式為:

pip install exifread

? ? ? ? 使用exifread.process_file獲取圖像的信息:

img_path = r"bei_012744.jpg"
f = open(img_path, 'rb')
contents = exifread.process_file(f)
f.close()

? ? ? ? 單步調試,contents內容如下:

GPS坐標轉換:

? ? ? ? 通過exifread獲取的經緯度信息格式通常是下面這樣的:緯度 [28, 56, 109097/5000] 經度 [112, 38, 436353/10000],轉換公式如下:

度 = 度 + 分/60 + 秒/3600?
[28, 56, 109097/5000] = 28 + 56 / 60 + 109097/5000 / 3600 = 28.93939427777778

? ? ? ? 因此坐標轉換代碼如下:

def convert_gps(coord_arr):
? ? arr = str(coord_arr).replace('[', '').replace(']', '').split(', ')
? ? d = float(arr[0])
? ? m = float(arr[1])
? ? s = float(arr[2].split('/')[0]) / float(arr[2].split('/')[1])
? ? return float(d) + (float(m) / 60) + (float(s) / 3600)

? ? ? ? 完整代碼:

import exifread
?
img_path = r"bei_012744.jpg"
f = open(img_path, 'rb')
contents = exifread.process_file(f)
f.close()
?
lon = contents['GPS GPSLongitude'].printable ?# 經度
lon = convert_gps(lon)
lat = contents['GPS GPSLatitude'].printable ?# 緯度
lat = convert_gps(lat)
altitude = contents['GPS GPSAltitude'].printable ?# 相對高度
altitude = float(altitude.split('/')[0]) / float(altitude.split('/')[1])
?
print("GPSLongitude:", lon, "\nGPSLatitude:", lat, "\naltitude:", altitude)

? ? ? ? 結果:

GPSLongitude: 112.64545425?
GPSLatitude: 28.93939427777778?
altitude: 58.009
?

原文鏈接:https://blog.csdn.net/weixin_34910922/article/details/123340837

欄目分類
最近更新