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()
실행, 난 당신이 스스로 그것을 알아낼 수 있다고 생각합니다.
출력 파일을 연 후 출력 파일을 ['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
[PEP 8 스타일 가이드 (Python 코드)] (https://www.python.org/dev/peps/pep-0008/)를 읽고 따르는 것이 좋습니다. – martineau
당신은'getEXIFdata()'함수를 호출하지 않습니다. 내부의 코드는 실행되지 않습니다. –