Python數(shù)據(jù)獲取實現(xiàn)圖片數(shù)據(jù)提取
比如我隨便從手機上傳一張圖片到我的電腦里,通過python可以獲取這張照片的所有信息。如果是數(shù)碼相機拍攝的照片,我們在屬性里可以找到照片拍攝的時間,拍攝的經(jīng)緯度,海拔高度。
那么這些信息有什么作用呢?
有很多功能…比如用戶畫像,客戶信息標簽設定等等,用戶喜歡拍攝照片的季節(jié),時間點,所使用的相機的參數(shù)指標可以反應出一個人的金錢狀況,對于其拍攝的內(nèi)容,我們可以通過AI的方式對照片的內(nèi)容信息進行提取,從而判斷一個人的興趣愛好。
一、利用exifread提取圖片的EXIF信息
exifread
介紹:
EXIF
信息,是可交換圖像文件的縮寫,是專門為數(shù)碼相機的照片設定的,可以記錄數(shù)碼照片的屬性信息和拍攝數(shù)據(jù)。EXIF可以附加于JPEG
、TIFF
、RIFF
等文件之中,為其增加有關(guān)數(shù)碼相機拍攝信息的內(nèi)容和索引圖或圖像處理軟件的版本信息。
首先要安裝ExifRead
:
pip3 install ExifRead
pic=r'D:\S072003Python\input\test\test.jpg' import exifread f = open(pic, 'rb') tags = exifread.process_file(f) print(tags) #內(nèi)有相機型號,拍攝時間,經(jīng)緯度等
tags
print(tags)和tags獲取數(shù)據(jù)的格式不同。
tags['Image ImageWidth'] tags['Image ImageLength'] tags['Image ExifOffset'] tags['Image Orientation'] tags['Image DateTime'] tags['EXIF WhiteBalance'] tags['EXIF ISOSpeedRatings'] tags['EXIF FocalLength'] tags['EXIF Flash'] tags['EXIF LightSource']
exifcolumns=['Image ImageWidth','Image ImageLength','Image ExifOffset','Image Orientation','Image DateTime','EXIF WhiteBalance','EXIF ISOSpeedRatings','EXIF FocalLength','EXIF Flash','EXIF LightSource'] # 把要提取的數(shù)據(jù)都封裝在列表當中
for i in range(len(exifcolumns)): print(tags[exifcolumns[i]]) # 使用循環(huán)拿到所有的數(shù)據(jù)
二、循環(huán)遍歷圖片信息
任務:一次性獲得以下圖片的"Image ImageWidth"
信息。寫一個循環(huán)即可:
import exifread import os import pandas as pd import glob pic_list=glob.glob(r'C:\Users\Lenovo\Pictures\Saved Pictures\*.jpg') # 如果是png,jpeg,bmp等數(shù)據(jù)格式,如何設置? for i in pic_list: fr=open(i,'rb') tags=exifread.process_file(fr) if "Image ImageWidth" in tags: # 條件判斷,因為并不是所有的照片都有"Image ImageWidth" print(tags["Image ImageWidth"])
# 經(jīng)緯度獲取 import exifread import os import pandas as pd import glob pic_list=glob.glob(r'C:\Users\Lenovo\Pictures\Saved Pictures\*.jpg') latlonlists=[] for i in pic_list: fr=open(i,'rb') tags=exifread.process_file(fr) if "GPS GPSLatitude" in tags: # 條件判斷,因為并不是所有的照片都有"Image ImageWidth" # 維度轉(zhuǎn)換 lat_ref=tags["GPS GPSLatitudeRef"] lat=tags["GPS GPSLatitude"].printable[1:-1].replace(" ","").replace("/",",").split(",") lat=float(lat[0])+float(lat[1])/60+float(lat[2])/3600 if lat_ref in ["N"]: # 表示是南半球的數(shù)據(jù) lat=lat*(-1) # 經(jīng)度轉(zhuǎn)換 lon_ref=tags["GPS GPSLongitudeRef"] lon=tags["GPS GPSLongitude"].printable[1:-1].replace("","").replace("/",",").split(",") lon=float(lon[0])+float(lon[1])/60+float(lon[2])/3600 if lon_ref in ["E"]: # 表示是西半球的數(shù)據(jù) lon=lon*(-1) print("維度:",lat,"經(jīng)度:",lon) latlonlist=[lat,lon] latlonlists.append(latlonlist)
到此這篇關(guān)于Python數(shù)據(jù)獲取實現(xiàn)圖片數(shù)據(jù)提取的文章就介紹到這了,更多相關(guān)Python 圖片數(shù)據(jù)提取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3內(nèi)置模塊pprint讓打印比print更美觀詳解
這篇文章主要給大家介紹了關(guān)于Python3內(nèi)置模塊pprint讓打印比print更美觀的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python3具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06Python ORM框架SQLAlchemy學習筆記之數(shù)據(jù)添加和事務回滾介紹
這篇文章主要介紹了Python ORM框架SQLAlchemy學習筆記之數(shù)據(jù)添加和事務回滾介紹,需要的朋友可以參考下2014-06-06