Exif information acquired pictures (shooting device access to information, time, location, etc.) with python

The first step: install

pip install exifread

 

The second part: the Code

Import exifread
 Import Requests
 class PhotoExifInfo ():
     DEF  __init__ (Self, photo_path): 
        self.photo_path = photo_path 
        self.baidu_map_ak = " " 

    DEF get_tags (Self):
         "" " Get Photos Information " "" 
        image_content = Open (self.photo_path , ' rb ' ) 
        Tags = exifread.process_file (image_content)
         "" " 
        # get photos traversing all the information 
        for J, k in tags.items (): 
            Print (J,k)
        """


        # Print photos and some information 
        Print ( ' shooting time: ' , Tags [ ' EXIF datetimeoriginal ' ])
         Print ( ' Camera manufacturer: ' , Tags [ ' Image the Make ' ])
         Print ( ' camera model: ' , Tags [ ' the Model Image ' ])
         Print ( ' picture size: ' , Tags [ ' the EXIF ExifImageWidth ' ], Tags [ ' the EXIF ExifImageLength ' ])

        image_content.close()
        return tags

    def get_lng_lat(self):
        """经纬度转换"""
        tags = self.get_tags()
        try:
            # 纬度
            LatRef = tags["GPS GPSLatitudeRef"].printable
            Lat = tags["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
            Lat = float(Lat[0]) + float(Lat[1]) / 60 + float(Lat[2]) / 3600
            if LatRef != "N":
                Lat = Lat * (-1)
            # 经度
            LonRef = tags["GPS GPSLongitudeRef"].printable
            Lon = tags["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
            Lon = float(Lon[0]) + float(Lon[1]) / 60 + float(Lon[2]) / 3600
            if LonRef != "E":
                Lon = Lon * (-1)
            return Lat,Lon
        except:
            print('Unable to get')

    def get_city_info(self):
        result = self.get_lng_lat()
        if result:
            Lat, Lon = result
            url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak="+self.baidu_map_ak+"&output=json&coordtype=wgs84ll&location=" + str(Lat) + ',' + str(Lon)
            response = requests.get(url).json()
            status = response['status']
            if status == 0:
                address = response['result']['formatted_address']
                return address
            else:
                print('baidu_map error')

if __name__ == '__main__':
    result = test = PhotoExifInfo("IMG_20190918_080329.jpg").get_city_info()
    print("拍摄地点:{}".format(result))

Note: Baidu interface to convert their latitude and longitude can go to apply

Guess you like

Origin www.cnblogs.com/lvye001/p/11543889.html