From 54fc57f633a55d1ba0fe413f5a7b4415bb7ac2e5 Mon Sep 17 00:00:00 2001 From: Nayam Amarshe <25067102+NayamAmarshe@users.noreply.github.com> Date: Fri, 9 Feb 2024 18:26:04 +0530 Subject: [PATCH] Fix #646 --- electron/commands/batch-upscayl.ts | 29 +++++++++++++++++++++++------ electron/commands/double-upscayl.ts | 1 - electron/commands/image-upscayl.ts | 1 - electron/utils/convert-and-scale.ts | 6 +----- electron/utils/logit.ts | 3 +++ 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/electron/commands/batch-upscayl.ts b/electron/commands/batch-upscayl.ts index 2a1cebe..f1cf2a1 100644 --- a/electron/commands/batch-upscayl.ts +++ b/electron/commands/batch-upscayl.ts @@ -145,20 +145,37 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => { files.forEach(async (file) => { if (file.startsWith(".") || file === outputFolderName) return; console.log("Filename: ", removeFileExtension(file)); + let upscaledImagePath = `${outputFolderPath}${slash}${removeFileExtension( + file, + )}.${saveImageAs}`; + if ( + isAlpha && + saveImageAs === "jpg" && + fs.existsSync( + `${outputFolderPath}${slash}${removeFileExtension(file)}.jpg.png`, + ) + ) { + console.log("This is an Alpha image!"); + upscaledImagePath = `${outputFolderPath}${slash}${removeFileExtension(file)}.jpg.png`; + } await convertAndScale( inputDir + slash + file, - isAlpha - ? `${outputFolderPath}${slash}${removeFileExtension(file)}.png` - : `${outputFolderPath}${slash}${removeFileExtension( - file, - )}.${saveImageAs}`, + upscaledImagePath, `${outputFolderPath}${slash}${removeFileExtension( file, )}.${saveImageAs}`, desiredScale, saveImageAs, - isAlpha, ); + if ( + isAlpha && + saveImageAs === "jpg" && + fs.existsSync( + `${outputFolderPath}${slash}${removeFileExtension(file)}.jpg.png`, + ) + ) { + fs.unlinkSync(upscaledImagePath); + } }); mainWindow.webContents.send( COMMAND.FOLDER_UPSCAYL_DONE, diff --git a/electron/commands/double-upscayl.ts b/electron/commands/double-upscayl.ts index 1f9630a..9974a34 100644 --- a/electron/commands/double-upscayl.ts +++ b/electron/commands/double-upscayl.ts @@ -161,7 +161,6 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => { outFile, desiredScale.toString(), saveImageAs, - isAlpha, ); mainWindow.setProgressBar(-1); mainWindow.webContents.send( diff --git a/electron/commands/image-upscayl.ts b/electron/commands/image-upscayl.ts index 1138295..7656937 100644 --- a/electron/commands/image-upscayl.ts +++ b/electron/commands/image-upscayl.ts @@ -176,7 +176,6 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => { outFile, desiredScale, saveImageAs, - isAlpha, ); // Remove the png file (default) if the saveImageAs is not png // fs.access( diff --git a/electron/utils/convert-and-scale.ts b/electron/utils/convert-and-scale.ts index 1cb596b..65fdf67 100644 --- a/electron/utils/convert-and-scale.ts +++ b/electron/utils/convert-and-scale.ts @@ -10,9 +10,8 @@ const convertAndScale = async ( processedImagePath: string, scale: string, saveImageAs: ImageFormat, - isAlpha: boolean, ) => { - if (!isAlpha && !customWidth && scale === "4" && compression === 0) { + if (!customWidth && scale === "4" && compression === 0) { logit("Skipping compression for 4x scale and 0% compression"); return; } @@ -36,7 +35,6 @@ const convertAndScale = async ( if (!originalImage) { throw new Error("Could not grab the original image!"); } - console.log("🚀 => originalImage:", originalImage); // Convert compression percentage (0-100) to compressionLevel (0-9) const compressionLevel = Math.round((compression / 100) * 9); @@ -71,8 +69,6 @@ const convertAndScale = async ( orientation: originalImage.orientation, }); - console.log("🚀 => newImage:", newImage); - const buffer = await newImage .withMetadata({ density: originalImage.density, diff --git a/electron/utils/logit.ts b/electron/utils/logit.ts index 23b4c64..a91a33f 100644 --- a/electron/utils/logit.ts +++ b/electron/utils/logit.ts @@ -6,6 +6,9 @@ const logit = (...args: any) => { const mainWindow = getMainWindow(); if (!mainWindow) return; log.log(...args); + if (process.env.NODE_ENV === "development") { + return; + } mainWindow.webContents.send(COMMAND.LOG, args.join(" ")); };