Auto-rotate images based on EXIF data

This commit is contained in:
2026-02-25 07:45:51 +09:00
parent 851772398c
commit b42da7603b
+30 -12
View File
@@ -606,6 +606,12 @@ class MainViewModel : BaseViewModel, IFontResolver, ICanCheckShutdown
reportTitlePar.Format.Font.Name = "Noto Sans JP"; // has english letters in it, too reportTitlePar.Format.Font.Name = "Noto Sans JP"; // has english letters in it, too
reportTitlePar.AddText(ReportTitle); reportTitlePar.AddText(ReportTitle);
// //
var convertedDir = Path.Combine(folderPath, "converted");
if (!Directory.Exists(convertedDir))
{
Directory.CreateDirectory(convertedDir);
}
//
GlobalFontSettings.FontResolver = this; GlobalFontSettings.FontResolver = this;
GlobalFontSettings.FallbackFontResolver = new FailsafeFontResolver(); GlobalFontSettings.FallbackFontResolver = new FailsafeFontResolver();
var hasAddedData = false; var hasAddedData = false;
@@ -661,16 +667,22 @@ class MainViewModel : BaseViewModel, IFontResolver, ICanCheckShutdown
var info = new FileInfo(file.FilePath); var info = new FileInfo(file.FilePath);
uint loadedImageWidth = 0; uint loadedImageWidth = 0;
uint loadedImageHeight = 0; uint loadedImageHeight = 0;
if (!isPDF)
{
using var mImage = new MagickImage(info.FullName);
var convertedOutputPath = Path.Combine(convertedDir, info.Name + ".jpg");
var didAdjust = false;
LogInfo("Image orientation of {0} is {1}", fileName, mImage.Orientation);
if (mImage.Orientation != OrientationType.TopLeft)
{
LogInfo("Auto-adjusted image orientation of {0}", fileName);
mImage.AutoOrient();
didAdjust = true;
}
// perform needed image manipulations
if (isHEIC || isWebp || isPNG || (!isPDF && info.Length > 2.5 * 1024 * 1024 /* 2.5 MB */)) if (isHEIC || isWebp || isPNG || (!isPDF && info.Length > 2.5 * 1024 * 1024 /* 2.5 MB */))
{ {
// Save image as jpg // Save image as jpg
var convertedDir = Path.Combine(folderPath, "converted");
if (!Directory.Exists(convertedDir))
{
Directory.CreateDirectory(convertedDir);
}
var outputPath = Path.Combine(convertedDir, info.Name + ".jpg");
using var mImage = new MagickImage(info.FullName);
loadedImageWidth = mImage.Width; loadedImageWidth = mImage.Width;
loadedImageHeight = mImage.Height; loadedImageHeight = mImage.Height;
mImage.Quality = 80; mImage.Quality = 80;
@@ -679,18 +691,24 @@ class MainViewModel : BaseViewModel, IFontResolver, ICanCheckShutdown
loadedImageWidth = (uint)Math.Floor(mImage.Width * 0.5); loadedImageWidth = (uint)Math.Floor(mImage.Width * 0.5);
loadedImageHeight = (uint)Math.Floor(mImage.Height * 0.5); loadedImageHeight = (uint)Math.Floor(mImage.Height * 0.5);
mImage.Scale(loadedImageWidth, loadedImageHeight); mImage.Scale(loadedImageWidth, loadedImageHeight);
LogInfo("Image {2} scaled to {0}x{1}", loadedImageWidth, loadedImageHeight, fileName);
} }
await mImage.WriteAsync(outputPath); didAdjust = true;
filePath = Path.Combine("Converted", info.Name + ".jpg"); LogInfo("Converted image {0} to JPEG", fileName);
LogInfo(string.Format("Converted image to JPEG; fileName is now {0}", file.FilePath));
} }
else if (!isPDF) else
{ {
// load height/width // load height/width
using var mImage = new MagickImage(info.FullName);
loadedImageWidth = mImage.Width; loadedImageWidth = mImage.Width;
loadedImageHeight = mImage.Height; loadedImageHeight = mImage.Height;
} }
if (didAdjust)
{
await mImage.WriteAsync(convertedOutputPath);
filePath = Path.Combine("Converted", info.Name + ".jpg");
LogInfo(string.Format("Saved adjusted image to JPEG; fileName is now {0}", file.FilePath));
}
}
var paragraph = section.AddParagraph(); var paragraph = section.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Center; paragraph.Format.Alignment = ParagraphAlignment.Center;
var image = paragraph.AddImage(filePath); var image = paragraph.AddImage(filePath);