preface

The text and pictures in this article come from the network, only for learning, exchange, do not have any commercial purposes, copyright belongs to the original author, if you have any questions, please contact us to deal with.

Author: Wang Xiangfeng Python

PS: If you need Python learning materials, please click on the link below to obtain them

Note.youdao.com/noteshare?i…

Get geographic location

The story ends there, but how did little Detective P parse his way to the exact spot in the photo? It would be a mess if everyone could figure out where others were based on photos. Don’t worry, if you want to parse the geographic location of photos sent by wechat through the code, you need to meet the following requirements:

2. GPS positioning is set by default when the camera takes a picture. 3.

Today’s phones have GPS turned on by default when taking pictures. Just make sure they don’t have an iPhone and ask them to send you a copy.

The longitude and latitude are stored in the photo property, but how can we reverse the location from longitude and latitude? At this point, we need to use the inverse geocoding tool of Baidu Map:

Detective’s code

import requests
import exifread


class GetPhotoInfo:
    def __init__(self, photo):
        self.photo = photo
        # Baidu Map AK
        self.ak = 'nYPs4LQ9a4VhVxj55AD69K6zgsRy9o4z'
        self.location = self.get_photo_info()

    def get_photo_info(self, ):
        with open(self.photo, 'rb') as f:
            tags = exifread.process_file(f)
        try:
            # Print a photo of some of this information
            print('Time taken:', tags['EXIF DateTimeOriginal'])
            print('Camera Maker:', tags['Image Make'])
            print('Camera Model:', tags['Image Model'])
            print('Photo size:', tags['EXIF ExifImageWidth'], tags['EXIF ExifImageLength'])
            # latitude
            lat_ref = 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]) / float(lat[3) /3600
            iflat_ref ! ="N":
                lat = lat * (- 1)
            # longitude
            lon_ref = 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]) / float(lon[3) /3600
            iflon_ref ! ="E":
                lon = lon * (- 1)
        except KeyError:
            return "ERROR: Please make sure the photo contains EXIF information such as latitude and longitude."
        else:
            print(Latitude and longitude:, lat, lon)
            return lat, lon

    def get_location(self):
        url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak={}&output=json' \
              '&coordtype=wgs84ll&location={},{}'.format(self.ak, *self.location)
        response = requests.get(url).json()
        status = response['status']
        if status == 0:
            address = response['result'] ['formatted_address']
            print('Full Address:', address)
        else:
            print('baidu_map error')


if __name__ == '__main__':
    Main = GetPhotoInfo('wechat picture _20191203180732.jpg')
    Main.get_location()
Copy the code