The target scene

Sometimes my girlfriend plays outside alone and asks her where she is, but won’t tell me. But what if you want to know where your girlfriend is?

In fact, you can do this to your girlfriend, pretend that you are bored at home, you can help her edit the picture, ask her wechat to send you the original picture, after getting the “wechat original picture”, you can use Python to quickly get the specific location of your girlfriend.



The preparatory work

First, install a library that recognizes image metadata in your virtual environment.

pip3 install exifreadCopy the code



Then, enter autonavi open platform, apply for a Web service application, and obtain a “Key” for reverse geocoding API.


Write a script

The whole operation is divided into three steps, namely, obtaining the longitude and latitude of the image, correcting the longitude and latitude data, and calling Autonavi inverse geocoding API to obtain the specific location.


Step 1: Get the longitude and latitude of the image.

Using the Exifread library, you can directly read the image file and obtain the metadata of the image, including the longitude, latitude, north and south latitude, east and west longitude and the time when the image was taken.

Use exifread to retrieve image metadata
img_exif = exifread.process_file(open(self.img_path, 'rb'))

Properties can be read
if img_exif:
     # number of latitude
     latitude_gps = img_exif['GPS GPSLatitude']

     # N,S in north and south latitudes
     latitude_direction = img_exif['GPS GPSLatitudeRef']

     # the degree
     longitude_gps = img_exif['GPS GPSLongitude']

     # E,W east-west longitude
     longitude_direction = img_exif['GPS GPSLongitudeRef']

     # Shooting time
     take_time = img_exif['EXIF DateTimeOriginal']Copy the code


If metadata exists, then determine if the shooting time is reasonable. If it’s not today, I’m sorry to inform you that your girlfriend is lying to you.

def judge_time_met(self, take_time):
    """Param take_time: :return:"""
    # Shooting time
    format_time = str(take_time).split("")[0].replace(":"."-")

    # Date of the day
    today = str(datetime.date.today())

    if format_time == today:
        return True
    else:
        return False

if is_lie:
        print('SORRY to inform you that your girlfriend is lying!! ')
        return
Copy the code


If she is not lying, go to step 2.

Because there is some error in longitude, latitude and coordinates of Amap obtained by GPS, the coordinates need to be converted to “Mars coordinate system”.

X_pi = 3.14159265358979324 * 3000.0/180.0 PI = 3.1415926535897932384626# PI.A = 6378245.0# semi-major axisEe = 0.00669342162296594323# flat rate

def wgs84togcj02(lng, lat):
    """WGS84 to GCJ02(Mars coordinates) : Param LNG: Longitude of WGS84 coordinates: Param Lat: latitude of WGS84 coordinates :return:"""
    if out_of_china(lng, lat):  # Determine if it is in the country
        returnLNG, LAT DLAT = TransformLAT (LNG-105.0, LAT-35.0) DLNG = TransformLLNG (LNG-105.0, LAT-35.0) Lat-35.0) radlat = Lat / 180.0 * PI magic = math.sin(radlat) magic = 1 - EE * magic * magic sqrtMagic = Math.sqrt (magic) dlAT = (dlAT * 180.0)/((a * (1-EE))/(Magic * SQrtMagic) * PI) DLNG = (DLNG * 180.0)/(a / sqrtmagic * math.cos(radlat) * pi) mglat = lat + dlat mglng = lng + dlngreturn [mglng, mglat]Copy the code


In addition, the longitude and latitude parameters in the interface can identify only six decimal places. You need to perform certain data processing for degrees, minutes, and seconds in the longitude and latitude before rounding them off.

def __format_lati_long_data(self, data):
    ""Param data: original latitude and longitude values :return:""
    Delete the left and right parentheses and Spaces
    data_list_tmp = str(data).replace('['.' ').replace('] '.' ').split(', ')
    data_list = [data.strip() for data in data_list_tmp]

    # Replace the value of seconds
    data_tmp = data_list[-1].split('/')

    The value of # s
    data_sec = int(data_tmp[0]) / int(data_tmp[1]) / 3600

    # Replace the value of the score
    data_tmp = data_list[-2]

    The value of # points
    data_minute = int(data_tmp) / 60

    The value of the # degrees
    data_degree = int(data_list[0])

    # Because the Autonavi API can only recognize 6 decimal places
    # needs to be converted to a floating point number and retained as a 6-digit decimal
    result = "%.6f" % (data_degree + data_minute + data_sec)
    return float(result)
Copy the code


Step 3: Call Autonavi’s anti-geocoding API, pass in the application Key, and get the detailed address of your girlfriend.

def __get_address(self, location):
    ""Param location: latitude and longitude value :return:""
    resp = requests.get(self.url_get_position.format(self.api_key, location))

    location_data = json.loads(resp.text)

    address = location_data.get('regeocode').get('formatted_address')

    return address
Copy the code


Results the conclusion

Make sure the picture is the original one and you can quickly tell if your girlfriend is lying. If she is not lying, return to her specific location.

I started this article on the public account “AirPython”. You can get the complete code by replying to “Image location”.

If you think the article is good, please like it and share it. Your affirmation is my biggest encouragement and support.


Recommended reading:

Automation – Building a customer service robot for Idle Fish (PART 1)

Pretty little sister on Tiktok, Python has been downloaded for you