Files
file-folder-helper/ADO2025/PI6/Helper-2025-07-05.cs

112 lines
4.9 KiB
C#

using System.Drawing;
using Microsoft.Extensions.Logging;
namespace File_Folder_Helper.ADO2025.PI6;
internal static partial class Helper20250705 {
internal static void ExportFaces(ILogger<Worker> logger, List<string> args) {
string checkFile;
logger.LogInformation(args[0]);
logger.LogInformation(args[1]);
logger.LogInformation(args[2]);
logger.LogInformation(args[3]);
logger.LogInformation(args[4]);
logger.LogInformation(args[5]);
logger.LogInformation(args[6]);
logger.LogInformation(args[7]);
string locationDigits = args[6];
string[] mp = args[4].Split('~');
string[] mwg = args[5].Split('~');
string[] xyz = args[3].Split(',');
string[] fileNames = args[2].Split('~');
string[] wholePercentages = args[7].Split('~');
string sourceDirectory = Path.GetFullPath(args[0].Split('~')[0]);
if (mp.Length != mwg.Length || mp.Length != wholePercentages.Length) {
logger.LogWarning("Lengths don't match!");
} else {
foreach (string fileName in fileNames) {
checkFile = Path.Combine(sourceDirectory, fileName);
if (!File.Exists(checkFile)) {
logger.LogWarning("<{file}> doesn't exist!", args[2]);
continue;
}
for (int i = 0; i < mp.Length; i++) {
if (wholePercentages[i].StartsWith("x-")) {
logger.LogWarning("Skipping {wholePercentages}", wholePercentages[i]);
continue;
}
Ext(checkFile, xyz, i, mp[i], mwg[i], locationDigits, wholePercentages[i]);
}
}
}
}
private static void Ext(string checkFile, string[] xyz, int i, string mp, string mwg, string locationDigits, string wholePercentages) {
int width = int.Parse(xyz[0]);
int height = int.Parse(xyz[1]);
string[] mpValues = mp.Split(',');
string[] mwgValues = mwg.Split(',');
float mpWidth = float.Parse(mpValues[2]) * width;
float mpHeight = float.Parse(mpValues[3]) * height;
double mpLeft = (float.Parse(mpValues[0]) * width) - (mpWidth * .5);
double mpTop = (float.Parse(mpValues[1]) * height) - (mpHeight * .5);
Extract(file: checkFile,
width: mpWidth,
height: mpHeight,
left: mpLeft,
top: mpTop,
suffix: $"-{i}mp.jpg");
float mwgWidth = float.Parse(mwgValues[2]) * width;
float mwgHeight = float.Parse(mwgValues[3]) * height;
double mwgLeft = double.Parse(mwgValues[0]) * width;
double mwgTop = double.Parse(mwgValues[1]) * height;
Extract(file: checkFile,
width: mwgWidth,
height: mwgHeight,
left: mwgLeft,
top: mwgTop,
suffix: $"-{i}mwg.jpg");
int length = (int.Parse(locationDigits) - 1) / 4;
string[] segments =
[
wholePercentages[..1],
wholePercentages.Substring(1, length),
wholePercentages.Substring(3, length),
wholePercentages.Substring(5, length),
wholePercentages.Substring(7, length)
];
if (string.Join(string.Empty, segments) == wholePercentages) {
if (int.TryParse(segments[1], out int xWholePercent)
&& int.TryParse(segments[2], out int yWholePercent)
&& int.TryParse(segments[3], out int wWholePercent)
&& int.TryParse(segments[4], out int hWholePercent)) {
float factor = 100;
RectangleF rectangleF = new(xWholePercent / factor, yWholePercent / factor, wWholePercent / factor, hWholePercent / factor);
float rectangleFWidth = rectangleF.Width * width;
float rectangleFHeight = rectangleF.Height * height;
double rectangleFLeft = rectangleF.Left * width;
double rectangleFTop = rectangleF.Top * height;
Extract(file: checkFile,
width: rectangleFWidth,
height: rectangleFHeight,
left: rectangleFLeft,
top: rectangleFTop,
suffix: $"-{i}whole-percentages.jpg");
}
}
}
private static void Extract(string file, float width, float height, double left, double top, string suffix) {
RectangleF rectangle = new((float)left, (float)top, width, height);
using (Bitmap source = new(file)) {
using (Bitmap bitmap = new((int)width, (int)height)) {
using (Graphics graphics = Graphics.FromImage(bitmap))
graphics.DrawImage(source, new RectangleF(0, 0, width, height), rectangle, GraphicsUnit.Pixel);
bitmap.Save($"{file}{suffix}");
}
}
}
}