basic image export!

This commit is contained in:
Colin McMillen 2023-07-28 15:52:36 -04:00
parent 3e4bd73091
commit 073c9745e9

View File

@ -11,6 +11,7 @@ using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Metadata.Profiles.Xmp; using SixLabors.ImageSharp.Metadata.Profiles.Xmp;
using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Drawing; using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Formats.Jpeg;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.CompilerServices; 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<Rgba32> image = Image.Load<Rgba32>(Filename)) {
image.Save(filename, encoder);
}
}
private bool TryParseRating(XmpProfile? xmp, out int rating) { private bool TryParseRating(XmpProfile? xmp, out int rating) {
rating = 0; rating = 0;
if (xmp == null) { if (xmp == null) {
@ -651,6 +667,10 @@ public class Game : GameWindow {
photoIndex -= 5; photoIndex -= 5;
} }
if (input.IsKeyPressed(Keys.X)) {
ExportPhotos();
}
// Make sure the photoIndex is actually valid. // Make sure the photoIndex is actually valid.
if (photos.Count == 0) { if (photos.Count == 0) {
photoIndex = 0; photoIndex = 0;
@ -781,10 +801,10 @@ public class Game : GameWindow {
// Load photos from a directory. // Load photos from a directory.
// string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\"); // 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(@"G:\DCIM\100EOSR6\");
// string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23"); // 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\"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\");
for (int i = 0; i < files.Count(); i++) { 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. // Start loading any images that are in our window but not yet loaded.
int minLoadedImage = Math.Max(0, photoIndex - 20); int minLoadedImage = Math.Max(0, photoIndex - 30);
int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1); int maxLoadedImage = Math.Min(photoIndex + 30, photos.Count - 1);
List<Photo> toLoad = new(); List<Photo> toLoad = new();
for (int i = minLoadedImage; i <= maxLoadedImage; i++) { for (int i = minLoadedImage; i <= maxLoadedImage; i++) {
lock (loadedImagesLock) { 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) { protected override void OnRenderFrame(FrameEventArgs e) {
base.OnRenderFrame(e); base.OnRenderFrame(e);
fpsCounter.Update(); fpsCounter.Update();