国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

用python找出那些被“標(biāo)記”的照片

2019-11-25 16:14:22
字體:
供稿:網(wǎng)友

源碼傳送門

環(huán)境準(zhǔn)備

下面的兩個(gè)第三方模塊都可以直接通過pip快速安裝,這里使用py36作為運(yùn)行環(huán)境。

思路

  1. 遍歷目錄
  2. 拉取數(shù)據(jù)集合
  3. 遍歷集合取得exif
  4. exif信息整理,并獲取實(shí)體地址
  5. 拷貝文件到結(jié)果樣本目錄
  6. 生成json報(bào)告文件

基礎(chǔ)知識

下面是現(xiàn)今相片中會存在與GPS相關(guān)的關(guān)鍵字,大牛亦可一比帶過~ [參考]

{ "GPSVersionID": "GPS版本", "GPSLatitudeRef": "南北緯", "GPSLatitude": "緯度", "GPSLongitudeRef": "東西經(jīng)", "GPSLongitude": "經(jīng)度", "GPSAltitudeRef": "海拔參照值", "GPSAltitude": "海拔", "GPSTimeStamp": "GPS時(shí)間戳", "GPSSatellites": "測量的衛(wèi)星", "GPSStatus": "接收器狀態(tài)", "GPSMeasureMode": "測量模式", "GPSDOP": "測量精度", "GPSSpeedRef": "速度單位", "GPSSpeed": "GPS接收器速度", "GPSTrackRef": "移動(dòng)方位參照", "GPSTrack": "移動(dòng)方位", "GPSImgDirectionRef": "圖像方位參照", "GPSImgDirection": "圖像方位", "GPSMapDatum": "地理測量資料", "GPSDestLatitudeRef": "目標(biāo)緯度參照", "GPSDestLatitude": "目標(biāo)緯度", "GPSDestLongitudeRef": "目標(biāo)經(jīng)度參照", "GPSDestLongitude": "目標(biāo)經(jīng)度", "GPSDestBearingRef": "目標(biāo)方位參照", "GPSDestBearing": "目標(biāo)方位", "GPSDestDistanceRef": "目標(biāo)距離參照", "GPSDestDistance": "目標(biāo)距離", "GPSProcessingMethod": "GPS處理方法名", "GPSAreaInformation": "GPS區(qū)功能變數(shù)名", "GPSDateStamp": "GPS日期", "GPSDifferential": "GPS修正"}

初始化

考慮到exifread的模塊中有大量的logging輸出,這里將它的level級別調(diào)到最高。 然后下邊的KEY是某站在高德地圖API的時(shí)候遺留下來的 我也很尷尬。。就當(dāng)福利了

import osimport timeimport jsonimport randomimport loggingimport requestsimport exifreadlogging.basicConfig(level=logging.CRITICAL)KEY = "169d2dd7829fe45690fabec812d05bc3"

主邏輯函數(shù)

def main(): # 預(yù)設(shè)后綴列表 types = ["bmp", "jpg", "tiff", "gif", "png"] #結(jié)果數(shù)據(jù)集合 picex = [] # 文件存儲路徑 saves = "$" + input("| SavePath: ").strip() # 文件搜索路徑 并遍歷所有文件返回文件路徑列表 pools = jpgwalk(input("| FindPath: "), types) #存儲目錄 savep = "%s/%s" % (os.getcwd().replace("http://", "/"), saves) if savep in pools: pools.remove(savep) # 遍歷數(shù)據(jù)集并獲取exif信息 for path in pools: res = getEXIF(path) if res:  picex.append(res) # 結(jié)果報(bào)告 print("| Result %s" % len(picex)) # 如果存在結(jié)果 保存結(jié)果到j(luò)son并講相關(guān)圖片復(fù)制到該目錄下 if picex: #創(chuàng)建目錄 if not os.path.exists(saves):  os.mkdir(saves) #生成一個(gè)4格縮進(jìn)的json文件  with open("%s/%s.json" % (saves, saves), "wb") as f:  f.write(json.dumps(picex, ensure_ascii=False, indent=4).encode("utf8")) #copy圖像到該目錄 for item in picex:  source_path = item["Filename"]  with open("%s/%s" % (saves, source_path.split("/")[-1]), "wb") as f_in:  with open(source_path, "rb") as f_out:   f_in.write(f_out.read())

遍歷方法

遍歷指定及其所有下級目錄,并返回全部的圖片的路徑集合,這里要注意的是每次掃描后的拷貝行為都會生成緩存,所以通過指定 $ 來避開。

# 獲取指導(dǎo)目錄全部的圖片路徑def jpgwalk(path, types): _start = time.time() _pools = [] # 遍歷該目錄 并判斷files后綴 如符合規(guī)則則拼接路徑 for _root, _dirs, _files in os.walk(path): _pools.extend([_root.replace("http://", "/") + "/" +   _item for _item in _files if _item.split(".")[-1].lower() in types and "$" not in _root]) #報(bào)告消耗時(shí)間 print("| Find %s /n| Time %.3fs" % (len(_pools), time.time() - _start)) return _pools

經(jīng)緯度格式化

度分秒轉(zhuǎn)浮點(diǎn),方便api調(diào)用查詢,因?yàn)榇嬖谝恍┰幃惖臄?shù)據(jù)比如 1/0,所以默認(rèn)返回0

def cg(i): try: _ii = [float(eval(x)) for x in i[1:][:-1].split(', ')] _res = _ii[0] + _ii[1] / 60 + _ii[2] / 3600 return _res except ZeroDivisionError: return 0

EXIF信息整理

考慮到大部分的設(shè)備還未開始支持朝向、速度、測量依據(jù)等關(guān)鍵字,這里暫時(shí)只使用比較常見的,如有需要的朋友可以自行添加。畢竟得到的信息越多對社工有更大的幫助。

def getEXIF(filepath): #基礎(chǔ)關(guān)鍵字 _showlist = [ 'GPS GPSDOP', 'GPS GPSMeasureMode', 'GPS GPSAltitudeRef', 'GPS GPSAltitude', 'Image Software', 'Image Model', 'Image Make' ] #GPS關(guān)鍵字 _XYlist = ["GPS GPSLatitude", "GPS GPSLongitude"] #時(shí)間關(guān)鍵字 _TimeList = ["EXIF DateTimeOrigina", "Image DateTime", "GPS GPSDate"] #初始化結(jié)果字典 _infos = { 'Filename': filepath } with open(filepath, "rb") as _files: _tags = None # 嘗試去的EXIF信息 try:  _tags = exifread.process_file(_files) except KeyError:  return # 判斷是否存在地理位置信息 _tagkeys = _tags.keys() if _tags and len(set(_tagkeys) & set(_XYlist)) == 2 and cg(str(_tags["GPS GPSLongitude"])) != 0.0:  for _item in sorted(_tagkeys):  if _item in _showlist:   _infos[_item.split()[-1]] = str(_tags[_item]).strip()  # 經(jīng)緯度取值  _infos["GPS"] = (cg(str(_tags["GPS GPSLatitude"])) * float(1.0 if str(_tags.get("GPS GPSLatitudeRef", "N")) == "N" else -1.0),    cg(str(_tags["GPS GPSLongitude"])) * float(1.0 if str(_tags.get("GPS GPSLongitudeRef", "E")) == "E" else -1.0))  # 獲取實(shí)體地址  _infos["address"] = address(_infos["GPS"])  # 獲取照片海拔高度  if "GPS GPSAltitudeRef" in _tagkeys:  try:   _infos["GPSAltitude"] = eval(_infos["GPSAltitude"])  except ZeroDivisionError:   _infos["GPSAltitude"] = 0  _infos["GPSAltitude"] = "距%s%.2f米" % ("地面" if int(   _infos["GPSAltitudeRef"]) == 1 else "海平面", _infos["GPSAltitude"])  del _infos["GPSAltitudeRef"]  # 獲取可用時(shí)間  _timeitem = list(set(_TimeList) & set(_tagkeys))  if _timeitem:  _infos["Dates"] = str(_tags[_timeitem[0]])  return _infos

地址轉(zhuǎn)換

一個(gè)簡單的爬蟲,調(diào)用高德地圖api進(jìn)行坐標(biāo)轉(zhuǎn)換,考慮到原本是跨域,這里添加基礎(chǔ)的反防爬代碼。這里有個(gè)小細(xì)節(jié),海外的一律都取不到(包括臺灣),可以通過更換googlemap的api來實(shí)現(xiàn)全球查詢。

def address(gps): global KEY try: # 隨機(jī)UA _ulist = [  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1",  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0",  "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; 360SE)",  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",  "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",  "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; Tablet PC 2.0; .NET4.0E)",  "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",  "Mozilla/5.0 (X11; U; Linux i686; rv:1.7.3) Gecko/20040913 Firefox/0.10",  "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; ja) Presto/2.10.289 Version/12.00",  "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36" ] # 偽造header _header = {  "User-Agent": random.choice(_ulist),  "Accept": "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01",  "Accept-Encoding": "gzip, deflate, sdch",  "Accept-Language": "zh-CN,zh;q=0.8",  "Referer": "http://www.gpsspg.com", } _res = requests.get(  "http://restapi.amap.com/v3/geocode/regeo?key={2}&s=rsv3&location={1},{0}&platform=JS&logversion=2.0&sdkversion=1.3&appname=http%3A%2F%2Fwww.gpsspg.com%2Fiframe%2Fmaps%2Famap_161128.htm%3Fmapi%3D3&csid=945C5A2C-E67F-4362-B881-9608D9BC9913".format(gps[0], gps[1], KEY), headers=_header, timeout=(5, 5)) _json = _res.json() # 判斷是否取得數(shù)據(jù) if _json and _json["status"] == "1" and _json["info"] == "OK":  # 返回對應(yīng)地址  return _json.get("regeocode").get("formatted_address") except Exception as e: pass

實(shí)例

運(yùn)行該代碼 然后輸入保存文件夾名和掃描位置即可

這邊可以看到8019張中有396張存在有效的地理位置,打碼的地方就不解釋了,各位老司機(jī)~后期打算加入圖像識別,和相似度識別。

下面給大家分享小編收集整理的python專題知識:

python基本語法 

python多線程學(xué)習(xí)教程 

python排序算法大全

以上所述是小編給大家介紹的用python找出那些被“標(biāo)記”的照片,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平潭县| 福鼎市| 桦南县| 延津县| 忻州市| 视频| 峨边| 临夏市| 巴林左旗| 高碑店市| 灵川县| 伽师县| 麻栗坡县| 永安市| 绥芬河市| 嘉兴市| 娱乐| 调兵山市| 博湖县| 丰宁| 尚义县| 启东市| 石台县| 盐城市| 青冈县| 环江| 固始县| 康平县| 天津市| 台中市| 启东市| 甘洛县| 阳信县| 苗栗县| 瑞金市| 新龙县| 司法| 张家港市| 方城县| 清流县| 黄大仙区|