From 073c9745e9b7cab5a0f942443c4153de7e57720c Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 28 Jul 2023 15:52:36 -0400 Subject: [PATCH] basic image export! --- Program.cs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/Program.cs b/Program.cs index e0ee263..2fc8b55 100644 --- a/Program.cs +++ b/Program.cs @@ -11,6 +11,7 @@ using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.Metadata.Profiles.Xmp; using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.Drawing; +using SixLabors.ImageSharp.Formats.Jpeg; using System; using System.Diagnostics; using System.Runtime.CompilerServices; @@ -220,6 +221,21 @@ public class Photo { } } + public void SaveAsJpeg(string outputRoot, JpegEncoder encoder) { + string directory = System.IO.Path.Combine( + outputRoot, + String.Format("{0:D4}", DateTimeOriginal.Year), + String.Format("{0:D2}", DateTimeOriginal.Month), + String.Format("{0:D2}", DateTimeOriginal.Day)); + Directory.CreateDirectory(directory); + string filename = System.IO.Path.Combine(directory, System.IO.Path.GetFileName(Filename)); + Console.WriteLine("saving " + filename); + // FIXME: update JPEG metadata. + using (Image image = Image.Load(Filename)) { + image.Save(filename, encoder); + } + } + private bool TryParseRating(XmpProfile? xmp, out int rating) { rating = 0; if (xmp == null) { @@ -651,6 +667,10 @@ public class Game : GameWindow { photoIndex -= 5; } + if (input.IsKeyPressed(Keys.X)) { + ExportPhotos(); + } + // Make sure the photoIndex is actually valid. if (photos.Count == 0) { photoIndex = 0; @@ -781,10 +801,10 @@ public class Game : GameWindow { // Load photos from a directory. // string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\"); - // string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\"); + string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\"); // string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23"); - string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000"); + // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\"); for (int i = 0; i < files.Count(); i++) { @@ -846,8 +866,8 @@ public class Game : GameWindow { } } // Start loading any images that are in our window but not yet loaded. - int minLoadedImage = Math.Max(0, photoIndex - 20); - int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1); + int minLoadedImage = Math.Max(0, photoIndex - 30); + int maxLoadedImage = Math.Min(photoIndex + 30, photos.Count - 1); List toLoad = new(); for (int i = minLoadedImage; i <= maxLoadedImage; i++) { lock (loadedImagesLock) { @@ -863,6 +883,16 @@ public class Game : GameWindow { } } + // To find the JPEG compression level of a file from the command line: + // $ identify -verbose image.jpg | grep Quality: + private void ExportPhotos() { + JpegEncoder encoder = new JpegEncoder() { Quality = 98 }; + string outputRoot = @"c:\users\colin\desktop\totte-output"; + foreach (Photo p in photos) { + p.SaveAsJpeg(outputRoot, encoder); + } + } + protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); fpsCounter.Update();