Python數(shù)據(jù)獲取實(shí)現(xiàn)圖片數(shù)據(jù)提取
比如我隨便從手機(jī)上傳一張圖片到我的電腦里,通過(guò)python可以獲取這張照片的所有信息。如果是數(shù)碼相機(jī)拍攝的照片,我們?cè)趯傩岳锟梢哉业秸掌臄z的時(shí)間,拍攝的經(jīng)緯度,海拔高度。
那么這些信息有什么作用呢?
有很多功能…比如用戶畫像,客戶信息標(biāo)簽設(shè)定等等,用戶喜歡拍攝照片的季節(jié),時(shí)間點(diǎn),所使用的相機(jī)的參數(shù)指標(biāo)可以反應(yīng)出一個(gè)人的金錢狀況,對(duì)于其拍攝的內(nèi)容,我們可以通過(guò)AI的方式對(duì)照片的內(nèi)容信息進(jìn)行提取,從而判斷一個(gè)人的興趣愛好。
一、利用exifread提取圖片的EXIF信息
exifread
介紹:
EXIF
信息,是可交換圖像文件的縮寫,是專門為數(shù)碼相機(jī)的照片設(shè)定的,可以記錄數(shù)碼照片的屬性信息和拍攝數(shù)據(jù)。EXIF可以附加于JPEG
、TIFF
、RIFF
等文件之中,為其增加有關(guān)數(shù)碼相機(jī)拍攝信息的內(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ī)型號(hào),拍攝時(shí)間,經(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ù)都封裝在列表當(dāng)中
for i in range(len(exifcolumns)): print(tags[exifcolumns[i]]) # 使用循環(huán)拿到所有的數(shù)據(jù)
二、循環(huán)遍歷圖片信息
任務(wù):一次性獲得以下圖片的"Image ImageWidth"
信息。寫一個(gè)循環(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ù)格式,如何設(shè)置? for i in pic_list: fr=open(i,'rb') tags=exifread.process_file(fr) if "Image ImageWidth" in tags: # 條件判斷,因?yàn)椴⒉皇撬械恼掌加?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: # 條件判斷,因?yàn)椴⒉皇撬械恼掌加?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ù)獲取實(shí)現(xiàn)圖片數(shù)據(jù)提取的文章就介紹到這了,更多相關(guān)Python 圖片數(shù)據(jù)提取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3內(nèi)置模塊pprint讓打印比print更美觀詳解
這篇文章主要給大家介紹了關(guān)于Python3內(nèi)置模塊pprint讓打印比print更美觀的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06關(guān)于python的第三方庫(kù)下載與更改方式
這篇文章主要介紹了關(guān)于python的第三方庫(kù)下載與更改方式,使用python的朋友都知道python有很多非常方便的第三方庫(kù)可以使用,那么如果下載這些第三方庫(kù)呢,今天小編就帶你們來(lái)看看2023-04-04Python ORM框架SQLAlchemy學(xué)習(xí)筆記之?dāng)?shù)據(jù)添加和事務(wù)回滾介紹
這篇文章主要介紹了Python ORM框架SQLAlchemy學(xué)習(xí)筆記之?dāng)?shù)據(jù)添加和事務(wù)回滾介紹,需要的朋友可以參考下2014-06-06python通過(guò)字典dict判斷指定鍵值是否存在的方法
這篇文章主要介紹了python通過(guò)字典dict判斷指定鍵值是否存在的方法,實(shí)例分析了Python中使用has_key及in判斷指定鍵值是否存在的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03