Created
March 31, 2013 11:20
-
-
Save dck-jp/5280326 to your computer and use it in GitHub Desktop.
Create EXIF Statics with LINQtoCSV library (modified)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LINQtoCSV; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
namespace FStatics | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string checkPath = @"F:\Pictures\"; | |
string intermediateCsvFileName = @"intermediate.csv"; | |
string outputCsvFileName = @"output.csv"; | |
string exiv2ExePath = @"exiv2.exe"; | |
CreateCsvFile(checkPath, intermediateCsvFileName, exiv2ExePath); | |
var exifs = LoadCsvFile(intermediateCsvFileName); | |
var outputStatics = new StringBuilder(); | |
var cameras = new List<string>(new string[] { "E3100", "DMC-LX2", "PENTAX K100D", "PENTAX Q", "PENTAX K-5" }); | |
for (DateTime dt = new DateTime(2007, 1, 1); dt < DateTime.Now; dt = dt.AddMonths(1)) | |
{ | |
var temp = | |
from exif in exifs | |
where exif.TimeStamp >= dt && exif.TimeStamp < dt.AddMonths(1) | |
where exif.Camera != null && cameras.Contains(exif.Camera) | |
select exif; | |
var data = temp.ToArray(); | |
for (int f = 1; f < 150; f += 1) | |
{ | |
var output = | |
from exif in data | |
where exif.FocalLength35mmEq >= f && exif.FocalLength35mmEq < f + 1 | |
select exif; | |
var count = output.Count(); | |
outputStatics.Append(count).Append(","); | |
} | |
Console.WriteLine(dt.ToString()); | |
outputStatics.AppendLine(); | |
} | |
File.WriteAllText(outputCsvFileName, outputStatics.ToString()); | |
} | |
private static IEnumerable<ExifMini> LoadCsvFile(string csvFileName) | |
{ | |
var inputFileDescription = new CsvFileDescription | |
{ | |
SeparatorChar = '\t', | |
EnforceCsvColumnAttribute = true, | |
FirstLineHasColumnNames = false | |
}; | |
var cc = new CsvContext(); | |
var exifs = cc.Read<ExifMini>(csvFileName, inputFileDescription); | |
return exifs.ToArray(); | |
} | |
private static void CreateCsvFile(string checkPath, string outputFileName, string exiv2ExePath) | |
{ | |
//1.指定したディレクトリ下のjpgを拾う | |
foreach (string file in Directory.EnumerateFiles(checkPath, "*.jpg", SearchOption.AllDirectories)) | |
{ | |
//Picasa3が自動生成するTempフォルダは除外 | |
if (file.Contains(".Picasa3Temp")) { continue; } | |
//2.各jpgに対して | |
//2-1.exifデータを取得 | |
//標準出力を横取り | |
string exiv2Output = StartExiv2(exiv2ExePath, file); | |
//各行を:でsplit後、Trim | |
var sb = new StringBuilder(); | |
var re35mmEq = new Regex(@"\(35 mm equivalent: ([0-9.]+) mm\)"); | |
new List<string>(exiv2Output.Replace("\r", "").Split('\n')).ForEach(data => | |
{ | |
if (data.Contains("File name") || data.Contains("Camera model")) | |
{ | |
sb.Append(data.Split(new char[] { ':' }, 2)[1].Trim()).Append("\t"); | |
} | |
else if (data.Contains("Image timestamp")) | |
{ | |
var d = data.Split(new char[] { ':' }, 2)[1]; | |
try | |
{ | |
var timeStamp = DateTime.ParseExact(d.Trim(), @"yyyy:MM:dd HH:mm:ss", null); | |
sb.Append(timeStamp.ToString()).Append("\t"); | |
} | |
catch | |
{ | |
sb.Append(new DateTime(2000,1,1).ToString()).Append("\t"); | |
} | |
} | |
else if (data.Contains("Focal length")) | |
{ | |
var focalLength35mmEq = ""; | |
if (re35mmEq.IsMatch(data)) | |
{ | |
var m = re35mmEq.Match(data); | |
focalLength35mmEq = m.Groups[1].Value; | |
} | |
else | |
{ | |
focalLength35mmEq = "0"; | |
} | |
sb.Append(focalLength35mmEq); | |
} | |
}); | |
sb.AppendLine(); | |
//2-2.csv形式でファイルに追記 | |
File.AppendAllText(outputFileName, sb.ToString()); | |
} | |
} | |
private static string StartExiv2(string exiv2ExePath, string targetFilePath) | |
{ | |
Process process = new Process(); | |
process.StartInfo.FileName = exiv2ExePath; | |
process.StartInfo.Arguments = "\"" + targetFilePath + "\""; | |
process.StartInfo.UseShellExecute = false; | |
process.StartInfo.RedirectStandardOutput = true; // 標準出力をリダイレクト | |
process.StartInfo.CreateNoWindow = false; // コンソールウィンドウを表示しない | |
process.Start(); | |
process.WaitForExit(); | |
string output = process.StandardOutput.ReadToEnd(); | |
process.Close(); | |
return output; | |
} | |
class ExifMini | |
{ | |
[CsvColumn(FieldIndex = 1)] | |
public string FileName { get; set; } | |
[CsvColumn(FieldIndex = 2, CanBeNull=true)] | |
public string Camera { get; set; } | |
[CsvColumn(FieldIndex = 3)] | |
public DateTime TimeStamp { get; set; } | |
[CsvColumn(FieldIndex = 5)] | |
public double FocalLength35mmEq { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment