zoukankan      html  css  js  c++  java
  • Python 读取照片的信息:拍摄时间、拍摄设备、经纬度等,以及根据经纬度通过百度地图API获取位置

    通过第三方库exifread读取照片信息。
    exifread官网:https://pypi.org/project/ExifRead/

    一、安装exifread
    pip install exifread

    二、读取照片信息,以及根据经纬度通过百度地图API获取位置

    import exifread
    import json
    import urllib.request
    
    # Open image file for reading (binary mode)
    f = open('001.jpg', 'rb')
    
    # Return Exif tags
    tags = exifread.process_file(f)
    
    '''
    #打印所有照片信息
    for tag in tags.keys():    
        print("Key: {}, value {}".format(tag, tags[tag]))
    '''
    
    #打印照片其中一些信息
    print('拍摄时间:', tags['EXIF DateTimeOriginal'])
    print('照相机制造商:', tags['Image Make'])
    print('照相机型号:', tags['Image Model'])
    print('照片尺寸:', tags['EXIF ExifImageWidth'], tags['EXIF ExifImageLength'])
    
    #获取经度或纬度
    def getLatOrLng(refKey, tudeKey):
        if refKey not in tags:
            return None
        ref=tags[refKey].printable
        LatOrLng=tags[tudeKey].printable[1:-1].replace(" ","").replace("/",",").split(",")
        LatOrLng=float(LatOrLng[0])+float(LatOrLng[1])/60+float(LatOrLng[2])/float(LatOrLng[3])/3600
        if refKey == 'GPS GPSLatitudeRef' and tags[refKey].printable != "N":
            LatOrLng=LatOrLng*(-1)
        if refKey == 'GPS GPSLongitudeRef' and tags[refKey].printable != "E":
            LatOrLng=LatOrLng*(-1)
        return LatOrLng
    
    #调用百度地图API通过经纬度获取位置
    def getlocation(lat,lng):   
        url = 'http://api.map.baidu.com/geocoder/v2/?location=' + lat + ',' + lng + '&output=json&pois=1&ak=申请的百度地图KEY'
        req = urllib.request.urlopen(url)
        res = req.read().decode("utf-8") 
        str = json.loads(res)
        #print(str)
        jsonResult = str.get('result')
        formatted_address = jsonResult.get('formatted_address')
        return formatted_address
    
    lat = getLatOrLng('GPS GPSLatitudeRef','GPS GPSLatitude') #纬度
    lng = getLatOrLng('GPS GPSLongitudeRef','GPS GPSLongitude') #经度
    print('纬度:{} 经度:{}'.format(lat, lng))
    
    location = getlocation(str(lat), str(lng))
    print('位置:{}'.format(location))
  • 相关阅读:
    Spring 4 MVC+Apache Tiles 3 Example
    深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)
    spring-boot-starter-parent 1.3.6.RELEASE
    Spring Boot with JSP and Tiles3
    更改linux的最大文件描述符限制
    Android SharedPreference最佳实践
    SQLite可视化管理工具
    android数据库sqlite可视化操作
    android网络通讯数据封装之 json
    PullToRrefresh自定义下拉刷新动画
  • 原文地址:https://www.cnblogs.com/gdjlc/p/11537776.html
Copyright © 2011-2022 走看看