繁体   English   中英

用 Python 去除 EXIF 文件(.txt 文件)中的空格

[英]Strip out spaces in EXIF file (.txt file) with Python

我有 .txt 文件(每个图像一个),其格式如下所示。 我不知道如何提取我感兴趣的信息,没有空格。

ExifTool Version Number         : 10.20
File Name                       : R0010023.tiff
Directory                       : C:/gtag/wf1313
File Size                       : 46 MB
File Modification Date/Time     : 2016:07:07 20:57:38+01:00
File Access Date/Time           : 2016:07:07 20:57:38+01:00
File Creation Date/Time         : 2016:07:04 21:18:17+01:00
File Permissions                : rw-rw-rw-
File Type                       : TIFF
File Type Extension             : tif
MIME Type                       : image/tiff
Exif Byte Order                 : Little-endian (Intel, II)
Image Width                     : 4928
Image Height                    : 3264
Bits Per Sample                 : 8 8 8
Compression                     : PackBits
Photometric Interpretation      : RGB
Image Description               : 
Make                            : RICOH IMAGING COMPANY, LTD.
Camera Model Name               : GR II
Strip Offsets                   : (Binary data 558 bytes, use -b option to extract)
Orientation                     : Horizontal (normal)
Samples Per Pixel               : 3
Rows Per Strip                  : 51
Strip Byte Counts               : (Binary data 447 bytes, use -b option to extract)
X Resolution                    : 72
Y Resolution                    : 72
Planar Configuration            : Chunky
Resolution Unit                 : inches
Software                        : GR Firmware Ver 01.02
Modify Date                     : 2016:06:21 13:09:52
XMP Toolkit                     : Image::ExifTool 10.20
Compressed Bits Per Pixel       : 3.2
Flash Fired                     : False
Flash Function                  : False
Flash Red Eye Mode              : False
Flash Return                    : No return detection
Interoperability Index          : R98 - DCF basic file (sRGB)
Y Cb Cr Positioning             : Centered
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Copyright                       : 
Exposure Time                   : 1/1250
F Number                        : 6.3
ISO                             : 100
Sensitivity Type                : Standard Output Sensitivity
Exif Version                    : 0230
Date/Time Original              : 2016:06:21 13:09:52
Create Date                     : 2016:06:21 13:09:52
Components Configuration        : Y, Cb, Cr, -
Aperture Value                  : 6.3
Brightness Value                : 8.6
Exposure Compensation           : 0
Max Aperture Value              : 2.8
Metering Mode                   : Multi-segment
Light Source                    : Shade
Maker Note Type                 : Rdc
Firmware Version                : 1.02
Recording Format                : JPEG
Exposure Program                : Manual
Drive Mode                      : Single-frame
White Balance                   : Shade
White Balance Fine Tune         : 0 0
Focus Mode                      : Manual
Auto Bracketing                 : Off
Macro Mode                      : Off
Flash Mode                      : Off
Flash Exposure Comp             : 0
Manual Flash Output             : Full
Full Press Snap                 : Off
Dynamic Range Expansion         : Off
Noise Reduction                 : Weak
Image Effects                   : Standard
Vignetting                      : Off
Toning Effect                   : Off
Hue Adjust                      : Off
Focal Length                    : 18.3 mm
AF Area X Position 1            : 632
AF Area Y Position 1            : 418
AF Area X Position              : 2435
AF Area Y Position              : 1610
AF Status                       : In Focus
AF Area Mode                    : Auto
Sensor Width                    : 4928
Sensor Height                   : 3264
Cropped Image Width             : 4928
Cropped Image Height            : 3264
Wide Adapter                    : Not Attached
Color Temp Kelvin               : 0
Crop Mode 35mm                  : Off
ND Filter                       : Off
WB Bracket Shot Number          : 0
User Comment                    : 
Flashpix Version                : 0100
Color Space                     : sRGB
Exif Image Width                : 4928
Exif Image Height               : 3264
Exposure Mode                   : Manual
Focal Length In 35mm Format     : 28 mm
Scene Capture Type              : Standard
Contrast                        : Normal
Saturation                      : Normal
Sharpness                       : Normal
Owner Name                      : 
Serial Number                   : (00000000)14100511
Lens Info                       : 18.3mm f/2.8
Lens Make                       : RICOH IMAGING COMPANY, LTD.
Lens Model                      : GR LENS
GPS Version ID                  : 2.3.0.0
GPS Latitude Ref                : xxxx
GPS Longitude Ref               : xxxx
GPS Altitude Ref                : Above Sea Level
GPS Time Stamp                  : 12:09:52
GPS Img Direction Ref           : True North
GPS Img Direction               : 228.21
GPS Date Stamp                  : 2016:06:21
GPS Pitch                       : 0.79
GPS Roll                        : 0.41
PrintIM Version                 : 0300
Aperture                        : 6.3
Flash                           : Off, Did not fire
GPS Altitude                    : 91.7 m Above Sea Level
GPS Date/Time                   : 2016:06:21 12:09:52Z
GPS Latitude                    : xx deg xx' x.xx" N
GPS Longitude                   : x deg x' xx.xx" W
GPS Position                    : xx deg xx' x.xx" N, x deg x' xx.xx" W
Image Size                      : 4928x3264
Megapixels                      : 16.1
Scale Factor To 35 mm Equivalent: 1.5
Shutter Speed                   : 1/1250
Circle Of Confusion             : 0.020 mm
Field Of View                   : 65.5 deg
Focal Length                    : 18.3 mm (35 mm equivalent: 28.0 mm)
Hyperfocal Distance             : 2.71 m
Light Value                     : 15.6

我有以下代码,

import glob
file_list = glob.glob("*.txt")

for file_ in file_list:
    saved_lines = []
    sfile = open(file_, "r")
    lines = sfile.readlines() #array of all lines
    for line in lines:
        for text in ['File Name', 'GPS Longitude', 'GPS Latitude', 'GPS Altitude', 'GPS Img Direction', 'GPS Pitch', 'GPS Roll']:
            if text in line:
                saved_lines.append(line)
    parsed = "".join(saved_lines) #reassemble the file
    with open("parsed.txt", "a") as ofile: #write your output
        ofile.write(parsed)

dict={}
sfile = open("R001.txt", "r")
list = sfile.readlines()
for i in list:
    dict[i.split(':')[0]] = ''.join(i.split(':')[1:])

我面临的挑战是我需要将数据格式化为以下格式(以便能够将其导入我想使用的程序中),

"#image latitude    longitude   altitude    yaw pitch   roll"
"R001.JPG   xx.xxxx x.xxxx  xxx.xx  319.9   8.2 -2.1"
"R002.JPG   xx.xxxx x.xxxx  xxx.xx  319.4   10.1    3.6"

因此,每张图像一行,上面的数据。

如上所述创建字典是一个很好的第一步(我认为)。 但是,字典很难调用,因为字典的每个成员在成员名称后都有不同数量的空格。 也就是说,文件名-----------------------:...等等。

有没有办法查找成员,不包括空格? 我已经尝试了命令strip()但由于:不能很好地工作。

为什么不直接让 Exiftool 以您想要的格式输出您想要的信息。 如果每条数据之间的分隔符是一个制表符(它看起来可能是),你可以指定你想要的标签和-T选项:
exif -T -FileName -GPSLongitude -GPSLatitude -GPSAltitude -GPSImgDirection -GPSPitch -GPSRoll FileOrDir >Output.txt

要更好地控制输出,请查看-p选项。

编辑:这是一个示例 printformat 文件,其输出将接近您的示例输出

#[HEAD]"#image latitude    longitude   altitude    yaw pitch   roll"
"$FileName  $GPSLongitude   $GPSLatitude    $GPSAltitude    $GPSImgDirection    $GPSPitch   $GPSRoll"

编辑 2:我使用了这样的命令(Format.Txt 仅包含我第一次编辑的文本)
exiftool.exe -p /path/to/Format.txt /Target/DirOrFiles
我的输出看起来像这样(没有俯仰或滚动,因为它们是非标准的,我的配置文件中没有它们):

"#image latitude    longitude   altitude    yaw pitch   roll"
"0615090217.jpg 116 deg 4' 22.08" W 35 deg 15' 59.37" N 286.9 m Above Sea Level 41.26       "
"20151212-12.12.12.jpg  68 deg 14' 4.56" W  12 deg 7' 2.60" N   286.9 m Above Sea Level 650     "
"DSC_0428test.jpg   82 deg 50' 11.97" W 34 deg 40' 35.21" N             "
"IMGP0713-XL.jpg    156 deg 34' 48.00" W    70 deg 19' 12.00" N             "
"IMG_1279.MOV   84 deg 36' 34.56" W 39 deg 15' 7.92" N  263.716 m           "
"Plugh.jpg  116 deg 4' 22.08" W 35 deg 15' 59.37" N 286.9 m Above Sea Level 41.26       "
"Test5.jpg  116 deg 4' 22.07" W 35 deg 15' 59.37" N 286.9 m Above Sea Level 41.26       "
"xyzzy.jpg  68 deg 14' 4.56" W  12 deg 7' 2.60" N   286.9 m Above Sea Level 650     "

你有几个选择; 前两个将是:

  • :上分割一行,然后剥离部分:

     name, value = line.split(':', 1) name, value = name.strip(), value.strip()

    请注意str.split()的第二个参数; 这限制了字符串被拆分的次数; using 1告诉 Python 仅在第一个:字符上拆分,将其他任何字符留在原处。

  • 使用正则表达式:

     import re name, value = re.split(r'\s+:\s+', line, 1) value = value.rstrip() # remove a remaining newline

    同样, 1限制了应用拆分的次数。

我知道这是一个老问题——但我看到多个响应者提到了制表符分隔输出的-T标志,但没有人提到-csv选项。

跑步:

$ exiftool -a -csv -u -g1 my-picture.jpg

将以 CSV 格式打印所有可用的标签及其值,非常方便解析; 您可以使用内置的 CSV 模块,甚至可以使用 Pandas 等更重的模块。

请参阅 wiki 的“输入输出文本格式”部分: https ://exiftool.org/exiftool_pod.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM