Created
May 30, 2017 09:27
-
-
Save jenyayel/538198877983bd7a0b55baceddb34d6f to your computer and use it in GitHub Desktop.
Exif date taken on from album name
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 System; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.Serialization; | |
using System.Text; | |
namespace ExifDateFixer | |
{ | |
class Program | |
{ | |
private static readonly DirectoryInfo _root = new DirectoryInfo(@"Y:\Photos\Cameras"); | |
private static readonly string[] _images = new string[] { ".jpeg", ".jpg", ".tiff", ".png" }; | |
static void Main(string[] args) | |
{ | |
var albums = _root.GetDirectories(); | |
var totalImages = 0; | |
var handledImages = 0; | |
for (int i = 0; i < albums.Length; i++) | |
{ | |
Console.WriteLine($"[{i + 1} of {albums.Length}] {albums[i].Name}"); | |
var date = parseAlbumName(albums[i].Name); | |
Console.WriteLine($"Extracted date: {date.ToShortDateString()}"); | |
var result = processAlbum(date, albums[i].GetFiles()); | |
totalImages += result.filteredImages; | |
handledImages += result.handledImages; | |
Console.WriteLine(); | |
} | |
Console.WriteLine($"Out of {totalImages} images set date to {handledImages}"); | |
Console.ReadKey(); | |
} | |
static private (int filteredImages, int handledImages) processAlbum(DateTime takenOn, FileInfo[] fileInfo) | |
{ | |
var filteredImages = fileInfo | |
.Where(i => _images.Contains(i.Extension?.ToLower())) | |
.ToList(); | |
var handledImages = 0; | |
foreach (var image in filteredImages) | |
{ | |
var shouldCleanUp = false; | |
using (var file = Image.FromFile(image.FullName)) | |
{ | |
var dateProp = file.PropertyItems.FirstOrDefault(a => a.Id == 0x9003); | |
if (dateProp == null) | |
{ | |
dateProp = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem)); | |
dateProp.Id = 0x9003; | |
dateProp.Type = 2; | |
dateProp.Value = Encoding.UTF8.GetBytes(takenOn.ToString("yyyy:MM:dd HH:mm:ss")); | |
dateProp.Len = dateProp.Value.Length; | |
file.SetPropertyItem(dateProp); | |
file.Save(image.FullName + "c"); | |
shouldCleanUp = true; | |
} | |
} | |
if(shouldCleanUp) | |
{ | |
handledImages++; | |
File.Delete(image.FullName); | |
File.Move(image.FullName + "c", image.FullName); | |
Console.WriteLine($"Updated date for {image.Name}"); | |
} | |
} | |
return (filteredImages.Count, handledImages); | |
} | |
private static DateTime parseAlbumName(string albumName) | |
{ | |
// album names can be: | |
// * 1995 - 1997, Army days | |
// * 2000, Wedding | |
// * 2003 - 05, Tel Aviv - Sea | |
// * 2010 - 10 | |
// * 2012 - 01,02, Antarctica trip | |
var datePart = albumName.Split(',')[0]; | |
var yearPart = datePart.Substring(0, 4); | |
var monthPart = "01"; | |
if (datePart.Length == 9) | |
monthPart = datePart.Substring(7, 2); | |
return new DateTime(Int32.Parse(yearPart), Int32.Parse(monthPart), 1, 0, 0, 0, DateTimeKind.Utc); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment