返回列表 发帖

[原创代码] PowerShell图片EXIF信息提取

拍摄的照片才有EXIF信息 ,可能支持:[.jpg] 、[.jpeg] 、[.gif]、[.png]、[.bmp]自己试
Add-Type -AssemblyName System.Drawing
function Get-ExifData {
    param([string]$ImagePath)
    try {
        $bitmap = [System.Drawing.Bitmap]::FromFile($ImagePath)
        $exifData = [ordered]@{}
        
        # EXIF属性ID与名称映射表(部分常见属性)
        $exifTagMap = @{
            0x010F = "生产厂商"               # 相机制造商
            0x0110 = "设备型号"               # 相机型号
            0x0131 = "固件版本"               # 固件/软件版本
    0x0213 = "色相配置"
            0x0112 = "图像方向"               # 旋转方向(1-8)
    0x0132 = "最后修改时间"            # 文件修改时间
    0x8769 = "EXIF信息块"             # EXIF主信息区偏移量
    0x829A = "曝光时间"              # 快门速度(秒)
    0x829D = "光圈系数"               # F值
    0x8827 = "感光度"                # ISO值
    0x9003 = "拍摄时间"               # 原始拍摄时间
    0x9004 = "数字化时间"             # 文件创建时间
   
    0x9201 = "快门速度值"             # APEX单位值
    0x9202 = "光圈值"                # APEX计算值
    0x9204 = "曝光补偿"               # EV偏移值
    0x9207 = "测光模式"               # 平均/中央/点测光
    0x9208 = "白平衡"                # 光源类型
    0x9209 = "闪光灯状态"             # 是否启用闪光
    0x920A = "物理焦距"              # 镜头实际焦距(mm)
    0x9290 = "原始日期时间数字化(秒)"            # 秒以下时间戳
    0x9291 = "原始日期时间(秒)"
    0x9292 = "日期时间(秒)"  
    0xA002 = "像素宽度"              # 图像横向像素数
    0xA003 = "像素高度"              # 图像纵向像素数
0x8889= "未定义"
  0x0000 = "GPS版本信息"        
  0x0001 = "纬度参考(N/S)"      
  0x0002 = "GPSLatitude"            
  0x0003 = "经度参考(E/W)"      
  0x0004 = "GPSLongitude"            
  0x0005 = "海拔参考"         
  0x0006 = "海拔值"      
    0x011A = "水平分辨率"            # X轴dpi
    0x011B = "垂直分辨率"            # Y轴dpi
    0x0128 = "分辨率单位"             # 2=英寸,3=厘米
    0x8822 = "曝光模式"              # 自动/手动/光圈优先
    0x9000 = "处理模式"               # 自定义渲染标识
    0x9101 = "像素排列"              # RGB/YUV排列方式
    0xA000 = "FlashPix版本"          # FlashPix兼容版本
    0x5041 = "互操作标识"             # DCF兼容标识
    0x9205 = "最大光圈"              # 镜头最大F值
    0x927C = "厂商注释"              # 二进制制造商数据
    0xA404 = "数字变焦"              # 数字变焦倍数
    0xA401 = "传感器类型"             # 直接拍摄/扫描
    0x001D = "拍摄时间"
     
  0x0011 = "GPS处理方式"
0x9C9B = "小米扩展信息"       # 小米相机专用标签
  0xA001 = "色彩空间"           # sRGB/Adobe RGB
  0xA217 = "拍摄场景"           # 标准/风景/夜景等
  0x9400 = "环境温度(℃)"      
  0x9401 = "环境湿度(%)"  
}
        foreach ($prop in $bitmap.PropertyItems) {
            $propName = if ($exifTagMap.ContainsKey($prop.Id)) {
                $exifTagMap[$prop.Id]
            } else {
                "Unknown_0x{0:X4}" -f $prop.Id
            }
            
            try {
                $value = switch ($prop.Type) {
                    2 {  # ASCII字符串
                        [System.Text.Encoding]::ASCII.GetString($prop.Value).TrimEnd("`0")
                    }
                    3 {  # 无符号短整型
                        [System.BitConverter]::ToUInt16($prop.Value, 0)
                    }
                    4 {  # 无符号长整型
                        [System.BitConverter]::ToUInt32($prop.Value, 0)
                    }
                    5 {  # 有理数(分数表示)
                        $num = [System.BitConverter]::ToUInt32($prop.Value, 0)
                        $den = [System.BitConverter]::ToUInt32($prop.Value, 4)
                        if ($den -ne 0) { $num / $den } else { 0 }
                    }
                    7 {  # 字节数组
                        [System.BitConverter]::ToString($prop.Value)
                    }
                    default {
                        [System.BitConverter]::ToString($prop.Value)
                    }
                }
                # 特殊处理GPS坐标
# 修改GPS坐标处理部分
if ($propName -match "GPS(Latitude|Longitude)") {
    $degrees = [BitConverter]::ToUInt32($prop.Value, 0) / 1
    $minutes = [BitConverter]::ToUInt32($prop.Value, 4) / 1
    $seconds = [BitConverter]::ToUInt32($prop.Value, 8) / 10000
    $value = "{0}°{1}'{2:F4}""{3}" -f $degrees, $minutes, $seconds, $prop.Value[12]
}
                $exifData[$propName] = $value
            } catch {
                $exifData[$propName] = "Error: $_"
            }
        }
        # 结果格式化输出
        $exifData.GetEnumerator() | Format-Table @{
            Label = "属性名称"
            Expression = { $_.Key }
        }, @{
            Label = "属性值"
            Expression = { $_.Value }
        }, @{
            Label = "描述"
            Expression = {
                switch ($_.Key) {
                    'Model'         { '型号' }
                    'Software'      { '软件' }
                    'DateTime'      { '日期和时间' }
                    'FNumber'       { '光圈值' }
                    'ExposureTime'   { '曝光时间(秒)' }
                    'ISOSpeedRatings'{ '感光度' }
                    'FocalLength'   { '焦距(毫米)' }
                    'GPSLatitude'   { '纬度(度,分,秒)' }
                    'GPSLongitude'  { '经度(度,分,秒)' }
                    default         { '无描述信息' }
                }
            }
        }
    } catch {
        Write-Error "处理文件时出错:$($_.Exception.Message)"
    } finally {
        if ($bitmap) { $bitmap.Dispose() }
    }
}
# 调用示例
Get-ExifData -ImagePath "$env:USERPROFILE\Desktop\IMG_20250327_163811.jpg"
pauseCOPY

返回列表