用python 获取照片的Exif 信息(获取拍摄设备,时间,地点等信息)

第一步:先安装

1
pip install exifread

 

第二部:上代码

复制代码
import exifread
import requests
class PhotoExifInfo():
    def __init__(self,photo_path):
        self.photo_path = photo_path
        self.baidu_map_ak = ""

    def get_tags(self):
        """获取照片信息"""
        image_content = open(self.photo_path, 'rb')
        tags = exifread.process_file(image_content)
        """
        # 遍历获取照片所有信息
        for j, k in tags.items():
            print(j, k)
        """


        #打印照片其中一些信息
        print('拍摄时间:', tags['EXIF DateTimeOriginal'])
        print('照相机制造商:', tags['Image Make'])
        print('照相机型号:', tags['Image Model'])
        print('照片尺寸:', tags['EXIF ExifImageWidth'], tags['EXIF ExifImageLength'])

        image_content.close()
        return tags

    def get_lng_lat(self):
        """经纬度转换"""
        tags = self.get_tags()
        try:
            # 纬度
            LatRef = tags["GPS GPSLatitudeRef"].printable
            Lat = tags["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
            Lat = float(Lat[0]) + float(Lat[1]) / 60 + float(Lat[2]) / 3600
            if LatRef != "N":
                Lat = Lat * (-1)
            # 经度
            LonRef = tags["GPS GPSLongitudeRef"].printable
            Lon = tags["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
            Lon = float(Lon[0]) + float(Lon[1]) / 60 + float(Lon[2]) / 3600
            if LonRef != "E":
                Lon = Lon * (-1)
            return Lat,Lon
        except:
            print('Unable to get')

    def get_city_info(self):
        result = self.get_lng_lat()
        if result:
            Lat, Lon = result
            url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak="+self.baidu_map_ak+"&output=json&coordtype=wgs84ll&location=" + str(Lat) + ',' + str(Lon)
            response = requests.get(url).json()
            status = response['status']
            if status == 0:
                address = response['result']['formatted_address']
                return address
            else:
                print('baidu_map error')

if __name__ == '__main__':
    result = PhotoExifInfo("IMG_20190918_080329.jpg").get_city_info()
    print("拍摄地点:{}".format(result))
复制代码

注:百度接口经纬度转换 自己可以去申请

posted @   lvye001  阅读(1738)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示