2016-10-13 4 views
0
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: ") 

이것은 내가 지금까지 가지고있는 것이지만 실제로 파일에 데이터를 가져 오는 방법을 이해하지 못합니다. 파일을 크레이트하지만 그냥 비어 있습니다. 나는 바닥을 돌아 다니려고 노력했지만, 나는 그걸 작동시키지 못하고있다. 나는 파이썬에 익숙하지 않으므로 내가해야 할 일을 암시 할 때 간단하게 유지할 수 있으면 제발.exif 데이터를 csv 파일로 추출하는 방법은 무엇입니까?

+0

출력 파일을 연 후 출력 파일을 ['csv.writer'] (https://docs.python.org/2/library/csv.html#csv.writer) 개체로 만든 다음 해당 개체를 호출합니다 생성하려는 csv의 모든 행에 대한 ['writerow()'] (https://docs.python.org/2/library/csv.html#csv.csvwriter.writerow) 메소드. 충분히 간단? – martineau

+0

[PEP 8 스타일 가이드 (Python 코드)] (https://www.python.org/dev/peps/pep-0008/)를 읽고 따르는 것이 좋습니다. – martineau

+0

당신은'getEXIFdata()'함수를 호출하지 않습니다. 내부의 코드는 실행되지 않습니다. –

답변

0

exif의 데이터를 가져 오려면 .value을 사용하여 데이터 값을 가져옵니다. 다음은 예제입니다. 이

python exif2csv.py -i wifi.jpg -o demo_csv.csv 

같은이 코드는 당신이 디렉토리에 루프 파일을 원하는 경우

# -*-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() 

실행, 난 당신이 스스로 그것을 알아낼 수 있다고 생각합니다.

관련 문제