How does a programmer obtain GPS information through a picture? Too simple to believe

How does a programmer obtain GPS information through a picture? Too simple to believe

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

The following article is from struggling cyanobacteria

Preface

How to get the GPS information of a picture with python? Today I recommend exifread this amazing library, not only GPS information, but almost all the information of the picture can be obtained, come in and have a look! !

How to get the GPS of the picture taken? Here we need the exifread library, which is used to extract GPS information. Just install it directly with pip install exifread.

In fact, not only GPS information can be obtained, but almost all information of the picture can be obtained. The role of exifread is actually to replace viewing image attributes! As shown below:

Here is a demonstration with camel photos taken in Sanya to see if we can locate Sanya.

Note: If the image is compressed, the binary information inside will be destroyed, and it must not be extracted!

The principle is: first read the picture in binary format, then extract the GPS information inside through the exifread library, and then print it out in a specific format, and finally copy the longitude and latitude information directly, in support of the longitude and latitude to check the location You can locate it by checking it on the map.

Binary example of reading pictures in exifread library:

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

Extension 1: In the later stage, we can directly convert the latitude and longitude into a specific location through the API interface provided by Baidu. You can try it yourself!

Extension 2: Of course, the detailed information of the properties of your right-click image also has this information.

If you want to keep it confidential, you can delete the information directly by clicking Delete Attributes and Personal Information.