├── 111.png ├── PicLocation.py ├── README.md └── requirements.txt /111.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayddq/PicLocation/4e71002cf3c0625bc0c02e3770882fa35a8e7885/111.png -------------------------------------------------------------------------------- /PicLocation.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import exifread, requests, os, optparse 3 | 4 | 5 | class Pic_Location: 6 | def __init__(self, pic_dir='', pic=''): 7 | self.pic_dir, self.pic = pic_dir, pic 8 | # 百度ak信息,可自行申请 9 | self.ak = 'YiSITcGEkm7z6ITj54gKVQbuTiQno7u8' 10 | 11 | def run(self): 12 | return self.dir_file() if self.pic_dir else [self.exifread_infos(self.pic)] 13 | 14 | # 遍历图片目录,非递归遍历获取图片信息数组 15 | def dir_file(self): 16 | files = [os.path.join(self.pic_dir, i) for i in os.listdir(self.pic_dir) if not os.path.isdir(i)] 17 | infos = [self.exifread_infos(pic) for pic in files] 18 | return infos 19 | 20 | # 获取照片时间、设备、经纬度信息 21 | # photo参数:照片文件路径 22 | def exifread_infos(self, photo): 23 | if not os.path.exists(photo): 24 | return {} 25 | f = open(photo, 'rb') 26 | tags = exifread.process_file(f) 27 | 28 | try: 29 | # 拍摄时间 30 | time = tags["EXIF DateTimeOriginal"].printable 31 | # 纬度 32 | LatRef = tags["GPS GPSLatitudeRef"].printable 33 | Lat = tags["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",") 34 | Lat = float(Lat[0]) + float(Lat[1]) / 60 + float(Lat[2]) / float(Lat[3]) / 3600 35 | if LatRef != "N": 36 | Lat = Lat * (-1) 37 | # 经度 38 | LonRef = tags["GPS GPSLongitudeRef"].printable 39 | Lon = tags["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",") 40 | Lon = float(Lon[0]) + float(Lon[1]) / 60 + float(Lon[2]) / float(Lon[3]) / 3600 41 | if LonRef != "E": 42 | Lon = Lon * (-1) 43 | # 拍摄设备 44 | device = tags["Image Make"].printable + tags["Image Model"].printable 45 | f.close() 46 | # 拍摄地址 47 | addr = self.get_location(Lon, Lat) 48 | return {'photo': photo, 'lon': Lon, 'lat': Lat, 'time': time, 'device': device, 'addr': addr} 49 | except: 50 | return {} 51 | 52 | # 根据经纬度获取地址信息 53 | def get_location(self, lon, lat): 54 | items = {'location': str(lat) + "," + str(lon), 'ak': self.ak, 'output': 'json'} 55 | header = {'Referer': '1.grayddq.top'} 56 | res = requests.get('http://api.map.baidu.com/geocoder/v2/', params=items, headers=header).json() 57 | return res['result']['formatted_address'] if res['status'] == 0 else "" 58 | 59 | 60 | if __name__ == '__main__': 61 | parser = optparse.OptionParser() 62 | parser.add_option("-d", "--dir", dest="dir", help=u"target dir,demo: -d /home/root/") 63 | parser.add_option("-p", "--pic", dest="pic", help=u"target photo,demo: -p 1.jpg") 64 | options, _ = parser.parse_args() 65 | if options.dir or options.pic: 66 | infos = Pic_Location(pic_dir=options.dir).run() if options.dir else Pic_Location(pic=options.pic).run() 67 | for info in infos: 68 | if len(info)>0: 69 | print (u"图片:%s\n拍摄设备:%s\n拍摄时间:%s\n拍摄地址:%s\n经度:%s\n纬度:%s\n******************" % ( 70 | info['photo'], info['device'], info['time'], info['addr'], info['lon'], info['lat'])) 71 | else: 72 | parser.print_help() 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PicLocation 0.1 2 | 3 | 这个脚本可以快速获取指定图片的拍摄GPS、拍摄设备、时间和地点等信息。其原为我某个程序的一个模块,由于此脚本会在某些社工追溯的场景下使用,所以独立提了出来。 4 | 5 | ## Author ## 6 | 7 | 咚咚呛 8 | 9 | 如有其他建议,可联系微信280495355 10 | 11 | ## Support ## 12 | 13 | 满足如下需求 14 | 15 | 1、获取指定图片的拍摄信息,如拍摄设备、拍摄地址、拍摄时间、GPS等 16 | 2、可指定图片目录,遍历获取大量图片的信息 17 | 18 | 19 | ## Dependencies ## 20 | > pip install -r requirements.txt 21 | 22 | 23 | ## Config ## 24 | 25 | 脚本使用配置如下 26 | 27 | 1、程序存在两个参数-d和-p,可以指定图片目录或者图片文件 28 | 2、比如执行命令: 29 | python PicLocation.py -d c:\\111 30 | python PicLocation.py -p 123.jpg 31 | 32 | ## Screenshot ## 33 | 34 | ![Screenshot](111.png) 35 | 36 | 37 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.19.1 2 | ExifRead==2.1.2 3 | --------------------------------------------------------------------------------