From d871b62400f50c3d5b73aa43fd2636b9ca788031 Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Mon, 28 Sep 2020 01:24:54 +0200 Subject: [PATCH] Added "Keep Folder Structure / Copy All To Root Dir" option for batch processing --- Code/ImageUtils/ImageProcessing.cs | 12 ++-- Code/Main/MainForm.Designer.cs | 88 ++++++++++++++++++++++++++---- Code/Main/MainForm.cs | 10 ++++ Code/Main/PostProcessingQueue.cs | 31 +++++++++-- Code/Main/Upscale.cs | 2 +- Code/UI/ExtensionMethods.cs | 7 +++ 6 files changed, 128 insertions(+), 22 deletions(-) diff --git a/Code/ImageUtils/ImageProcessing.cs b/Code/ImageUtils/ImageProcessing.cs index 4d25f5a..09cfeec 100644 --- a/Code/ImageUtils/ImageProcessing.cs +++ b/Code/ImageUtils/ImageProcessing.cs @@ -17,7 +17,7 @@ namespace Cupscale { internal class ImageProcessing { - public enum Format { Source, PngOpti, PngFast, PngRaw, Jpeg, Weppy, BMP, TGA, DDS } + public enum Format { Source, Png50, PngFast, PngRaw, Jpeg, Weppy, BMP, TGA, DDS } public static Upscale.Filter currentFilter = Upscale.Filter.Mitchell; public static Upscale.ScaleMode currentScaleMode = Upscale.ScaleMode.Percent; @@ -59,7 +59,7 @@ public static async Task ConvertImagesToOriginalFormat(bool postprocess, bool se { if (GetTrimmedExtension(file2) == "png") break; - Format format = Format.PngOpti; + Format format = Format.Png50; if (GetTrimmedExtension(file2) == "jpg" || GetTrimmedExtension(file2) == "jpeg") format = Format.Jpeg; @@ -98,7 +98,7 @@ public static async Task ConvertImageToOriginalFormat(string path, bool postproc //if (GetTrimmedExtension(file2) == "png") //break; - Format format = Format.PngFast; + Format format = Format.Png50; if (GetTrimmedExtension(file2) == "jpg" || GetTrimmedExtension(file2) == "jpeg") format = Format.Jpeg; @@ -151,10 +151,10 @@ public static async Task ConvertImage(string path, Format format, bool fillAlpha img.Format = MagickFormat.Png; img.Quality = 0; } - if (format == Format.PngOpti) + if (format == Format.Png50) { img.Format = MagickFormat.Png; - img.Quality = 70; + img.Quality = 50; } if (format == Format.PngFast) { @@ -248,7 +248,7 @@ public static async Task PostProcessImage(string path, Format format, bool batch string ext = "png"; if (format == Format.Source) ext = Path.GetExtension(path).Replace(".", ""); - if (format == Format.PngOpti) + if (format == Format.Png50) { img.Format = MagickFormat.Png; img.Quality = 70; diff --git a/Code/Main/MainForm.Designer.cs b/Code/Main/MainForm.Designer.cs index fe90ed6..6024a6e 100644 --- a/Code/Main/MainForm.Designer.cs +++ b/Code/Main/MainForm.Designer.cs @@ -93,6 +93,10 @@ private void InitializeComponent() this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); this.panel9 = new System.Windows.Forms.Panel(); this.tableLayoutPanel9 = new System.Windows.Forms.TableLayoutPanel(); + this.label7 = new System.Windows.Forms.Label(); + this.panel12 = new System.Windows.Forms.Panel(); + this.label15 = new System.Windows.Forms.Label(); + this.batchOutMode = new System.Windows.Forms.ComboBox(); this.label13 = new System.Windows.Forms.Label(); this.batchOutDir = new System.Windows.Forms.TextBox(); this.panel10 = new System.Windows.Forms.Panel(); @@ -139,6 +143,7 @@ private void InitializeComponent() this.tableLayoutPanel3.SuspendLayout(); this.panel9.SuspendLayout(); this.tableLayoutPanel9.SuspendLayout(); + this.panel12.SuspendLayout(); this.panel10.SuspendLayout(); this.tableLayoutPanel8.SuspendLayout(); this.tableLayoutPanel6.SuspendLayout(); @@ -807,8 +812,8 @@ private void InitializeComponent() // this.tableLayoutPanel9.ColumnCount = 1; this.tableLayoutPanel9.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); - this.tableLayoutPanel9.Controls.Add(this.label13, 0, 0); - this.tableLayoutPanel9.Controls.Add(this.batchOutDir, 0, 1); + this.tableLayoutPanel9.Controls.Add(this.label7, 0, 0); + this.tableLayoutPanel9.Controls.Add(this.panel12, 0, 1); this.tableLayoutPanel9.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel9.Location = new System.Drawing.Point(0, 0); this.tableLayoutPanel9.Margin = new System.Windows.Forms.Padding(0); @@ -816,18 +821,73 @@ private void InitializeComponent() this.tableLayoutPanel9.RowCount = 2; this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F)); this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.tableLayoutPanel9.Size = new System.Drawing.Size(291, 481); this.tableLayoutPanel9.TabIndex = 13; // + // label7 + // + this.label7.AutoSize = true; + this.label7.ForeColor = System.Drawing.Color.White; + this.label7.Location = new System.Drawing.Point(8, 8); + this.label7.Margin = new System.Windows.Forms.Padding(8); + this.label7.Name = "label7"; + this.label7.Size = new System.Drawing.Size(49, 14); + this.label7.TabIndex = 12; + this.label7.Text = "Options"; + // + // panel12 + // + this.panel12.Controls.Add(this.label15); + this.panel12.Controls.Add(this.batchOutMode); + this.panel12.Controls.Add(this.label13); + this.panel12.Controls.Add(this.batchOutDir); + this.panel12.Dock = System.Windows.Forms.DockStyle.Fill; + this.panel12.Location = new System.Drawing.Point(0, 30); + this.panel12.Margin = new System.Windows.Forms.Padding(0); + this.panel12.Name = "panel12"; + this.panel12.Size = new System.Drawing.Size(291, 451); + this.panel12.TabIndex = 11; + // + // label15 + // + this.label15.AutoSize = true; + this.label15.ForeColor = System.Drawing.Color.White; + this.label15.Location = new System.Drawing.Point(8, 57); + this.label15.Margin = new System.Windows.Forms.Padding(8, 2, 8, 2); + this.label15.Name = "label15"; + this.label15.Size = new System.Drawing.Size(79, 15); + this.label15.TabIndex = 15; + this.label15.Text = "Output Mode"; + // + // batchOutMode + // + this.batchOutMode.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.batchOutMode.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); + this.batchOutMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.batchOutMode.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.batchOutMode.ForeColor = System.Drawing.Color.White; + this.batchOutMode.FormattingEnabled = true; + this.batchOutMode.Items.AddRange(new object[] { + "Keep Folder Structure", + "Place All Images In Root Directory"}); + this.batchOutMode.Location = new System.Drawing.Point(8, 76); + this.batchOutMode.Margin = new System.Windows.Forms.Padding(8, 2, 8, 2); + this.batchOutMode.Name = "batchOutMode"; + this.batchOutMode.Size = new System.Drawing.Size(275, 23); + this.batchOutMode.TabIndex = 14; + this.batchOutMode.SelectedIndexChanged += new System.EventHandler(this.batchOutMode_SelectedIndexChanged); + // // label13 // this.label13.AutoSize = true; this.label13.ForeColor = System.Drawing.Color.White; - this.label13.Location = new System.Drawing.Point(8, 8); - this.label13.Margin = new System.Windows.Forms.Padding(8); + this.label13.Location = new System.Drawing.Point(8, 7); + this.label13.Margin = new System.Windows.Forms.Padding(8, 2, 8, 2); this.label13.Name = "label13"; - this.label13.Size = new System.Drawing.Size(96, 14); - this.label13.TabIndex = 11; + this.label13.Size = new System.Drawing.Size(96, 15); + this.label13.TabIndex = 13; this.label13.Text = "Output Directory"; // // batchOutDir @@ -836,8 +896,8 @@ private void InitializeComponent() | System.Windows.Forms.AnchorStyles.Right))); this.batchOutDir.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); this.batchOutDir.ForeColor = System.Drawing.Color.White; - this.batchOutDir.Location = new System.Drawing.Point(8, 38); - this.batchOutDir.Margin = new System.Windows.Forms.Padding(8); + this.batchOutDir.Location = new System.Drawing.Point(8, 28); + this.batchOutDir.Margin = new System.Windows.Forms.Padding(8, 4, 8, 4); this.batchOutDir.Name = "batchOutDir"; this.batchOutDir.Size = new System.Drawing.Size(275, 23); this.batchOutDir.TabIndex = 10; @@ -1264,6 +1324,8 @@ private void InitializeComponent() this.panel9.ResumeLayout(false); this.tableLayoutPanel9.ResumeLayout(false); this.tableLayoutPanel9.PerformLayout(); + this.panel12.ResumeLayout(false); + this.panel12.PerformLayout(); this.panel10.ResumeLayout(false); this.tableLayoutPanel8.ResumeLayout(false); this.tableLayoutPanel8.PerformLayout(); @@ -1319,13 +1381,17 @@ private void InitializeComponent() private ComboBox postResizeFilter; private GroupBox groupBox2; private ComboBox postResizeMode; - private TableLayoutPanel tableLayoutPanel9; - private Label label13; - private TextBox batchOutDir; private TableLayoutPanel tableLayoutPanel8; private CheckBox postResizeOnlyDownscale; private Panel panel11; private HTAlt.WinForms.HTButton openOutFolderBtn; private HTAlt.WinForms.HTButton saveMergedPreviewBtn; + private TableLayoutPanel tableLayoutPanel9; + private Label label7; + private Panel panel12; + private Label label13; + private TextBox batchOutDir; + private Label label15; + private ComboBox batchOutMode; } } diff --git a/Code/Main/MainForm.cs b/Code/Main/MainForm.cs index 97e1a2d..5bed43e 100644 --- a/Code/Main/MainForm.cs +++ b/Code/Main/MainForm.cs @@ -46,6 +46,8 @@ private async void MainForm_Load(object sender, EventArgs e) UIHelpers.InitCombox(postResizeScale, 1); UIHelpers.InitCombox(postResizeMode, 0); UIHelpers.FillEnumComboBox(postResizeFilter, typeof(Upscale.Filter), 0); + // Batch Upscale + UIHelpers.InitCombox(batchOutMode, 0); await CheckInstallation(); } @@ -400,5 +402,13 @@ private async void saveMergedPreviewBtn_Click(object sender, EventArgs e) string finalPath = IOUtils.ReplaceInFilename(outFilename, "[temp]", ""); MessageBox.Show("Saved to " + finalPath + ".", "Message"); } + + private void batchOutMode_SelectedIndexChanged(object sender, EventArgs e) + { + if (batchOutMode.SelectedIndex == 0) + PostProcessingQueue.copyMode = PostProcessingQueue.CopyMode.KeepStructure; + if (batchOutMode.SelectedIndex == 1) + PostProcessingQueue.copyMode = PostProcessingQueue.CopyMode.CopyToRoot; + } } } diff --git a/Code/Main/PostProcessingQueue.cs b/Code/Main/PostProcessingQueue.cs index 4330204..635e0ae 100644 --- a/Code/Main/PostProcessingQueue.cs +++ b/Code/Main/PostProcessingQueue.cs @@ -22,6 +22,9 @@ class PostProcessingQueue public static bool ncnn; + public enum CopyMode { KeepStructure, CopyToRoot } + public static CopyMode copyMode; + public static void Start (string outpath) { currentOutPath = outpath; @@ -102,14 +105,34 @@ public static async Task ProcessQueue () if(Upscale.overwriteMode == Upscale.Overwrite.Yes) { string suffixToRemove = "-" + Program.lastModelName.Replace(":", ".").Replace(">>", "+"); - Logger.Log("[Remove Suffix] Copying " + outFilename + " to " + Path.Combine(currentOutPath, Path.GetFileName(outFilename).Replace(suffixToRemove, ""))); - File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename).Replace(suffixToRemove, "")), true); + if (copyMode == CopyMode.KeepStructure) + { + string relPath = outFilename.Replace(Paths.imgOutPath, ""); + string combinedPath = currentOutPath + relPath; + Logger.Log("combinedPath = " + combinedPath); + Directory.CreateDirectory(combinedPath.GetParentDir()); + File.Copy(outFilename, combinedPath.ReplaceInFilename(suffixToRemove, "", true)); + } + if (copyMode == CopyMode.CopyToRoot) + { + File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename).Replace(suffixToRemove, "")), true); + } File.Delete(outFilename); } else { - Logger.Log("[Keep Suffix] Copying " + outFilename + " to " + Path.GetFileName(outFilename)); - File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename)), true); + if (copyMode == CopyMode.KeepStructure) + { + string relPath = outFilename.Replace(Paths.imgOutPath, ""); + string combinedPath = currentOutPath + relPath; + Logger.Log("combinedPath = " + combinedPath); + Directory.CreateDirectory(combinedPath.GetParentDir()); + File.Copy(outFilename, combinedPath, true); + } + if (copyMode == CopyMode.CopyToRoot) + { + File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename)), true); + } File.Delete(outFilename); } BatchUpscaleUI.upscaledImages++; diff --git a/Code/Main/Upscale.cs b/Code/Main/Upscale.cs index 8929ee5..081d065 100644 --- a/Code/Main/Upscale.cs +++ b/Code/Main/Upscale.cs @@ -123,7 +123,7 @@ public static async Task PostprocessingSingle (string path, bool batchProcessing if (outputFormat.Text == ExportFormats.PNG.ToStringTitleCase()) { //path = Path.ChangeExtension(path, "png"); - await ImageProcessing.PostProcessImage(path, ImageProcessing.Format.PngFast, batchProcessing); + await ImageProcessing.PostProcessImage(path, ImageProcessing.Format.Png50, batchProcessing); } if (outputFormat.Text == ExportFormats.SameAsSource.ToStringTitleCase()) await ImageProcessing.ConvertImageToOriginalFormat(path, true, false, batchProcessing); diff --git a/Code/UI/ExtensionMethods.cs b/Code/UI/ExtensionMethods.cs index 42cfcf9..c8e9fd3 100644 --- a/Code/UI/ExtensionMethods.cs +++ b/Code/UI/ExtensionMethods.cs @@ -82,5 +82,12 @@ public static Image Scale (this Image img, float scale, InterpolationMode filter { return ImageOperations.Scale(img, scale, filtering); } + + public static string ReplaceInFilename(string path, string find, string replaceWith) + { + string parentDir = path.GetParentDir(); + string filename = Path.GetFileName(path); + return Path.Combine(parentDir, filename.Replace(find, replaceWith)); + } } }