From f7803f7c9e44c5047e4ad61c7e6497f5d1a48028 Mon Sep 17 00:00:00 2001 From: Nayam Amarshe <25067102+NayamAmarshe@users.noreply.github.com> Date: Mon, 15 Jan 2024 18:39:46 +0530 Subject: [PATCH] Add support for alpha channel in batch upscayl --- electron/commands/batch-upscayl.ts | 12 +++++++++--- electron/commands/double-upscayl.ts | 17 ++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/electron/commands/batch-upscayl.ts b/electron/commands/batch-upscayl.ts index 67c32de..e1de7e0 100644 --- a/electron/commands/batch-upscayl.ts +++ b/electron/commands/batch-upscayl.ts @@ -84,6 +84,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => { setStopped(false); let failed = false; + let isAlpha = false; const onData = (data: any) => { if (!mainWindow) return; @@ -97,6 +98,9 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => { failed = true; upscayl.kill(); } + if (data.includes("has alpha channel")) { + isAlpha = true; + } }; const onError = (data: any) => { if (!mainWindow) return; @@ -138,9 +142,11 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => { console.log("Filename: ", removeFileExtension(file)); await convertAndScale( inputDir + slash + file, - `${outputFolderPath}${slash}${removeFileExtension( - file - )}.${saveImageAs}`, + isAlpha + ? `${outputFolderPath}${slash}${removeFileExtension(file)}.png` + : `${outputFolderPath}${slash}${removeFileExtension( + file + )}.${saveImageAs}`, `${outputFolderPath}${slash}${removeFileExtension( file )}.${saveImageAs}`, diff --git a/electron/commands/double-upscayl.ts b/electron/commands/double-upscayl.ts index ba740da..cde4235 100644 --- a/electron/commands/double-upscayl.ts +++ b/electron/commands/double-upscayl.ts @@ -26,6 +26,7 @@ import { DoubleUpscaylPayload } from "../../common/types/types"; import { ImageFormat } from "../utils/types"; import getModelScale from "../../common/check-model-scale"; import showNotification from "../utils/show-notification"; +import { unlinkSync } from "fs"; const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => { const mainWindow = getMainWindow(); @@ -162,16 +163,14 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => { mainWindow.setProgressBar(-1); mainWindow.webContents.send( COMMAND.DOUBLE_UPSCAYL_DONE, - isAlpha - ? (outFile + ".png").replace( - /([^/\\]+)$/i, - encodeURIComponent((outFile + ".png").match(/[^/\\]+$/i)![0]) - ) - : outFile.replace( - /([^/\\]+)$/i, - encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]) - ) + outFile.replace( + /([^/\\]+)$/i, + encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]) + ) ); + if (isAlpha && saveImageAs === "jpg") { + unlinkSync(outFile + ".png"); + } showNotification("Upscayled", "Image upscayled successfully!"); } catch (error) { logit("❌ Error reading original image metadata", error);