前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python - 获取图像Exif详细信息

Python - 获取图像Exif详细信息

作者头像
为为为什么
发布2022-08-04 14:57:20
1K0
发布2022-08-04 14:57:20
举报
文章被收录于专栏:又见苍岚又见苍岚

相机拍摄照片会在文件中记录拍摄的重要参数,包括机身信息、光圈、焦距、感光度、时间、拍摄地点等等。本文介绍获取此类信息的方法。

安装python包

安装exifread:

代码语言:javascript
复制
pip install exifread

上代码

代码语言:javascript
复制
import exifread
import requests


class PhotoExifInfo():
    
    def __init__(self,photo_path):
        self.photo_path = photo_path
        self.baidu_map_ak = "your baidu map api key"
        self.image_info_dict={}
        self.tags ={}
        self.interested_keys = [
            'EXIF ExposureMode',\
            'EXIF ExposureTime',\
            'EXIF Flash',\
            'EXIF ISOSpeedRatings',\
            'Image Model',\
            'EXIF ExifImageWidth',\
            'EXIF ExifImageLength',\
            'Image DateTime',\
            'EXIF DateTimeOriginal',\
            'Image Make',\

            # lens
            # jiaoju
        ]
        
        
    def get_tags(self):
        """
        获取照片信息
        """
        image_content = open(self.photo_path, 'rb')
        tags = exifread.process_file(image_content)
        self.tags = tags
        
        for item in self.interested_keys:
            try:
                info = tags[item]
                self.image_info_dict[item] = info
            except:
                print(f'{self.photo_path} has no attribute of {item}')                
                continue
            
        # 遍历获取照片所有信息
        #for j, k in tags.items():
            #print(f"{j} : {k}")
            #print('拍摄时间:', tags['EXIF DateTimeOriginal'])
            #print('照相机制造商:', tags['Image Make'])
            #print('照相机型号:', tags['Image Model'])
            #print('照片尺寸:', tags['EXIF ExifImageWidth'], tags['EXIF ExifImageLength'])
            
     
        image_content.close()
       

    def get_lng_lat(self):
        """经纬度转换"""
        tags = self.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 = "https://api.map.baidu.com/reverse_geocoding/v3/?ak="+self.baidu_map_ak+"&output=json&coordtype=wgs84ll&location=" + str(Lat) + ',' + str(Lon)
            #url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak="+self.baidu_map_ak+"&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194"
            response = requests.get(url).json()
            status = response['status']
            if status == 0:
                address = response['result']['formatted_address']
                if address != "":
                    self.image_info_dict['Position'] = address
            else:
                print('baidu_map error')
    
    
    
    def get_image_info(self):
        self.get_tags()
        self.get_city_info()
        return self.image_info_dict
    
    
    
if __name__ == '__main__':
    result = PhotoExifInfo("test.jpeg").get_image_info()
    
    for j, k in result.items():
        print(f"{j} : {k}")

示例输出

代码语言:javascript
复制
EXIF ExposureMode : Auto Exposure
EXIF ExposureTime : 10/24723
EXIF Flash : Flash did not fire, auto mode
EXIF ISOSpeedRatings : 50
Image Model : vivo Z1
EXIF ExifImageWidth : 4144
EXIF ExifImageLength : 1968
Image DateTime : 2019:11:03 11:34:24
EXIF DateTimeOriginal : 2019:11:03 11:34:24
Image Make : vivo
Position : 上海市黄浦区中山南路187

代码中的self.baidu_map_ak需要去百度地图申请,具体方法移步: 申请百度地图API Key进行百度地图开发,获取经纬度对应地点

获取源码

文中测试环境与所有源码可在Github下载。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020年4月1日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装python包
  • 上代码
  • 获取源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档