Reading the gps information from a image, using selenium or installing安装 PyQt5 to show the address

  1. A image including GPS information

2.

#Anaconda Prompt 
#pip install exifread
import exifread
import re
import datetime

def convertToDegree(value):
    #GPS GPSLatitude :  [40, 44, 609/25]
                              #40
    d = float(value.values[0].num) / float(value.values[0].den)
                              #44
    m = float(value.values[1].num) / float(value.values[1].den)
                                #609/25
    s = float(value.values[2].num) / float(value.values[2].den)
    
    return d + (m / 60.0) + (s / 3600.0)

def  getGPSInfoFromImage(imagePath):
        GPS = {}
        date = ''
        # Open image file for reading (binary mode)
        f = open(imagePath,'rb')
        #Return Exif tags
        tags = exifread.process_file(f)
        #for key in tags:                           
        #        print(key,": ",tags[key])
        print('*'*100, '\n')
              
        latitude = tags.get('GPS GPSLatitude')
        if latitude: #if latitude exists
                #40°
            d=str(latitude.values[0].num) +'°'
                #44'
            m=str(latitude.values[1].num) +"'"
                                           #609 / 25 ==24.36"
            s=str( float(latitude.values[2].num) / float(latitude.values[2].den) ) +'"'
            print("GPS GPSLatitude(纬度) =", tags['GPS GPSLatitudeRef'], d+m+s )
            latitude=convertToDegree(latitude)
            #call values since #print(type(tags['GPS GPSLatitudeRef']))  #<class 'exifread.classes.IfdTag'>
            if (tags.get('GPS GPSLatitudeRef')).values != 'N':
                latitude=-latitude
        else:
            return {} #since we need both GPSLatitude and GPSLongitude to locate the position
            
        longitude = tags.get('GPS GPSLongitude')
        if longitude:#if longitude exists
            d=str(longitude.values[0].num) +'°'
            m=str(longitude.values[1].num) +"'"
            s=str( float(longitude.values[2].num) / float(longitude.values[2].den) ) +'"'
            print("GPS GPSLongitude(经度) =", tags['GPS GPSLongitudeRef'],d+m+s )
            longitude = convertToDegree(longitude)
            #call values since #print(type(tags['GPS GPSLongitudeRef']))  #<class 'exifread.classes.IfdTag'>
            if (tags.get('GPS GPSLongitudeRef')).values != 'E':
                longitude=-longitude
        else:
            return {} #since we need both GPSLatitude and GPSLongitude to locate the position
        
        createdTime = tags.get('Image DateTime') #it always exist
        if tags['Image DateTime']:
                print("filming time =",createdTime)
        return{'latitude':latitude,'longitude':longitude, 'createdTime':str(createdTime)}

info=getGPSInfoFromImage("H:\\101CLOUD\IMG_0785.jpg")
info

扫描二维码关注公众号,回复: 8894703 查看本文章

import pandas as pd
dataframe=pd.DataFrame( columns=info.keys() )
dataframe.loc[0]=list(info.values())

dataframe

#using the webiste to locate the position with latitude(40.7401) and longitude(-73.983383)

http://www.gpsspg.com/maps.htm

40.7401,  -73.983383

#https://www.google.com/maps

40.7401,  -73.983383

N 40°44'24.36"  W 73°59'0.18"

#using pyqt5 to locate the position

#Anaconda Prompt 
#pip install gmplot

import gmplot
gmap = gmplot.GoogleMapPlotter(center_lat=dataframe['latitude'],center_lng=dataframe['longitude'], zoom=18)
gmap.plot(dataframe.latitude,dataframe.longitude)
gmap.draw('./user001_map.html')

#we have to install PyQt5 in the Anaconda prompt and I not suggest you to install the PyQt5 by using Anaconda Navigator's Environments since it may cost a lot of time or enconter failure

#Besides, my Anaconda Navigator with python 3.6.3 currently not support upgrading pyqt to 5.6.0 and sip to 4.18.1

# so I went to https://pypi.org/project/PyQt5/#history to Release version and I am not suggest you to install the latest version since the bug may appear

#you also can go to the website https://www.riverbankcomputing.com/software/pyqt/download5/ for searching your version you want to install

# another user's install PyQt5 experience https://jingyan.baidu.com/article/4f7d57120937c41a21192744.html

or https://jingyan.baidu.com/article/425e69e6193279be15fc162d.html

#the following is about my PyQt5's install process

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl

app = QApplication([])
view = QWebEngineView()
view.load(QUrl("
C://Users/LlQ/0practices/user001_map.html"))
view.show()
app.exec_()

#Using selenium to call google maps

from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.select import Select

url = 'https://www.google.com/maps'
#from fake_useragent import UserAgent
userAgent=UserAgent()
header = {'UserAgent':userAgent.random,
          'Connection':'close'#countryNameRaw
         }

#from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("'user-agent=" + header['UserAgent'] + "'")
options.add_argument('--diable-gpu') # google document mentioned this attribute can avoid some bugs
options.add_experimental_option('excludeSwitches',['enable-automation'])

browser = webdriver.Chrome(executable_path='D:/chromedriver/chromedriver',
                           chrome_options=options
                         )
#from selenium.webdriver.support.ui import WebDriverWait
wait=WebDriverWait(browser, 2)
browser.get(url)
#from selenium.webdriver.support import expected_conditions as EC
#from selenium.webdriver.common.by import By
wait.until(EC.presence_of_all_elements_located( (By.ID, 'gs_lc50') )) #locate to input box by ID

searchInputBox=browser.find_element_by_id('searchboxinput')
#import time
#or browser.implicitly_wait(.5)
time.sleep(.5)
searchInputBox.click() #google map has autofocus function, so we may don't need
#searchInputBox.send_keys('40.7401,-73.983383')
searchInputBox.send_keys(
    str(dataframe.loc[0,'latitude']) +','+ str(dataframe.loc[0,'longitude'])
)                         

#locates search button
searchButton=browser.find_element_by_id('searchbox-searchbutton')
#from selenium.webdriver.support.select import Select
searchButton.click()
time.sleep(10)
browser.quit()

发布了53 篇原创文章 · 获赞 38 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Linli522362242/article/details/102714670