如何将exif数据提取到csv文件中?

2024-05-29 11:30:45 发布

您现在位置:Python中文网/ 问答频道 /正文

import pyexiv2
import os

print "Enter the full path to the directory that your images are conatined in."
print "-------------------------------------------"
newFileObj = open('C:\\users\\wilson\\desktop\\Metadata.csv', 'w')
targ_dir = raw_input('Path: ')

targ_files = os.listdir(targ_dir)

def getEXIFdata (imageFile):
    if imageFile.endswith(".db"):
        f = 1
    else:

        EXIFData = pyexiv2.ImageMetadata(imageFile)
        EXIFData.read()
        CamMake = EXIFData['Exif.Image.Make']
        DateTime = EXIFData['Exif.Image.DateTime']
        CamModel = EXIFData['Exif.Image.Model']
    for image in targ_files:
        getEXIFdata(targ_dir+"\\"+img)
        newFileObj.write(DateTime+' , '+CamMake+' , '+CamModel+'\r\n')
newFileObj.close()

end = raw_input("Press Enter to Finish: ")

这就是我目前所拥有的,但是我不知道如何真正地将数据放入文件中。它把文件装进箱子里,但它是空白的。我试过在底部移动,但似乎无法使其发挥作用。我是python新手,所以请您在提示我应该做什么时保持简单。在


Tags: thetoimageimportdatetimeosdirexif
1条回答
网友
1楼 · 发布于 2024-05-29 11:30:45

如果要获取exif的数据,请使用.value来获取数据的值。 这里有一个例子。在

# -*-coding:utf-8-*-
import sys
import csv
import os
import argparse
import pyexiv2

def main():
    parser = argparse.ArgumentParser(description="Change the txt file to csv.")
    parser.add_argument("-i", action="store", dest="infile")
    parser.add_argument("-o", action="store", dest="outfile")
    parser_argument = parser.parse_args()

    fatherdir = os.getcwd()  # code directory
    inputfile = outputfile = None

    # input exif file
    if parser_argument.infile:
        infilepaths = os.path.split(parser_argument.infile)
        # 'C:\User\lenovo\Desktop\pakistan.txt'  -> ['C:\User\lenovo\Desktop','pakistan.txt']
        if infilepaths[0]:  # full path
            inputfile = parser_argument.infile
            fatherdir = infilepaths[0]
        # 'pakistan.txt'  -> ['','pakistan.txt']
        else:  # only file name
            inputfile = fatherdir + '/' + parser_argument.infile
    # output csv file
    if parser_argument.outfile:
        outfilepaths = os.path.split(parser_argument.outfile)
        if outfilepaths[0]:  # full path
            outputfile = parser_argument.outfile
        else:
            outputfile = fatherdir + '/' + parser_argument.outfile
    else:
        outputfile = fatherdir + '/test_csv.csv'
    parse(inputfile, outputfile)


def parse(inputfile, outputfile):
    csvcontent = file(outputfile, 'wb')
    writer = csv.writer(csvcontent)

    exif_data = getEXIFdata(inputfile)
    writer.writerow([exif_data['Exif.Image.Orientation'].value,
                     exif_data['Exif.Photo.PixelXDimension'].value,
                     exif_data['Exif.Photo.PixelYDimension'].value])
    # for line in open(inputfile).readlines():
    #     writer.writerow([a for a in line.split('\t')])

    csvcontent.close()

def getEXIFdata (imageFile):
    if imageFile.endswith(".db"):
        print 'Skip this file'
    else:
        exif_data = pyexiv2.ImageMetadata(imageFile)
        exif_data.read()
        for s, v in exif_data.items():
            print s, v
        cam_a = exif_data['Exif.Image.Orientation'].value
        cam_b = exif_data['Exif.Photo.PixelXDimension'].value
        cam_c = exif_data['Exif.Photo.PixelYDimension'].value
        # add exif value
        ekey = 'Exif.Photo.UserComment'
        evalue = 'A comment.'
        exif_data[ekey] = pyexiv2.ExifTag(ekey, evalue)
        #metadata.write()
        return exif_data

if __name__ == '__main__':
    main()

像这样运行这个代码

^{pr2}$

如果你想在目录中循环文件,我想你可以自己解决。在

相关问题 更多 >

    热门问题