Python Technology-Obtain GPS information of pictures, lock and track the location and time of the picture

We need exifread library, which is used to extract GPS information. Just install it directly with pip install exifread .

I used this camel photo taken by my friend in Sanya some time ago as a demonstration to see if I can locate Sanya. [It was taken with an iPhone , you need to try whether other phones can extract GPS information]
Note: If the picture is compressed, the binary information inside will be destroyed, and it must not be extracted!
Insert picture description here
Our principle is: first read the picture in binary format, then extract the GPS information inside through the exifread library , then print it out in a specific format, and finally directly copy the latitude and longitude information inside [map query requires the latitude Put the front, so I will type the latitude first] You can locate it by checking it on a map that supports checking the location by latitude and longitude.

The specific code is as follows:

import exifread
import re

# 读取图片为二进制格式
f = open("luotuo.JPG","rb")
tags = exifread.process_file(f)

# GPS信息
GPS = {
    
    }

# 拍摄时间
Data = ""

for tag,value in tags.items():
    # 获取纬度信息
    if re.match('GPS GPSLatitude', tag):
        try:
            match_result=re.match('\[(\w*), (\w*), (\w.*)/(\w.*)\]', str(value)).groups()
            GPS['纬度'] = str(int(match_result[0])) + " " + str(int(match_result[1])) + " " + str(int(match_result[2])/int(match_result[3]))
        except:
            GPS['纬度'] = str(value)
    # 获取纬度信息
    elif re.match('GPS GPSLongitude', tag):
        try:
            match_result=re.match('\[(\w*), (\w*), (\w.*)/(\w.*)\]',str(value)).groups()
            GPS['经度'] = str(int(match_result[0])) + " " + str(int(match_result[1])) + " " + str(int(match_result[2])/int(match_result[3]))
        except:
            GPS['经度'] = str(value)
    # 获取高度
    elif re.match('GPS GPSAltitude', tag):
        GPS['高度'] = str(value)
    # 获取拍摄时间
    elif re.match('Image DateTime', tag):
        Data = str(value)

# 打印信息
print("纬 经 度:" + GPS['纬度'] + "," + GPS['经度'])
print("拍摄时间:" + Data)

As shown in the figure, the latitude and longitude information after reading is directly displayed.
Insert picture description here
Then search it out in this GPSSPG map.
You can see that the map directly locates Sanya City, Hainan Province.
Insert picture description here
Extension 1: Later, we can directly convert the latitude and longitude into a specific location through the API interface provided by Baidu . You can try it yourself, or wait for the blogger's update later!

Extension 2: Of course, this information is also in the detailed information of your right-click image properties.
If you want to keep it confidential, you can delete the information directly by clicking Delete Attributes and Personal Information.
Insert picture description here
And these information can be edited, can be used to deceive opponents, hey, you know how to play specifically!
I put my picture iphone11 into iphone12 up.
Insert picture description here
Like it if you like it ❤!

Guess you like

Origin blog.csdn.net/qq_38161040/article/details/103080034