1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-01 02:27:16 +01:00
upscayl/electron/commands/image-upscayl.ts

210 lines
5.8 KiB
TypeScript
Raw Normal View History

2023-09-10 11:14:04 +02:00
import fs from "fs";
2023-09-10 20:00:49 +02:00
import { modelsPath } from "../utils/get-resource-paths";
2023-09-10 11:14:04 +02:00
import COMMAND from "../constants/commands";
import {
2023-09-17 13:37:57 +02:00
compression,
2023-09-10 19:42:18 +02:00
customModelsFolderPath,
folderPath,
2023-09-18 20:30:43 +02:00
noImageProcessing,
2023-09-10 19:42:18 +02:00
outputFolderPath,
overwrite,
saveOutputFolder,
2023-09-10 11:14:04 +02:00
setChildProcesses,
setOverwrite,
2023-09-10 19:42:18 +02:00
setStopped,
stopped,
2023-09-10 11:14:04 +02:00
} from "../utils/config-variables";
import convertAndScale from "../utils/convert-and-scale";
import { getSingleImageArguments } from "../utils/get-arguments";
import logit from "../utils/logit";
import slash from "../utils/slash";
import { spawnUpscayl } from "../utils/spawn-upscayl";
import { parse } from "path";
import DEFAULT_MODELS from "../constants/models";
2023-09-10 19:42:18 +02:00
import { getMainWindow } from "../main-window";
2023-09-10 11:14:04 +02:00
const imageUpscayl = async (event, payload) => {
2023-09-10 19:42:18 +02:00
const mainWindow = getMainWindow();
if (!mainWindow) {
2023-09-11 04:30:50 +02:00
logit("No main window found");
2023-09-10 19:42:18 +02:00
return;
}
2023-09-10 19:59:35 +02:00
setOverwrite(payload.overwrite);
2023-09-10 19:42:18 +02:00
2023-09-10 11:14:04 +02:00
const model = payload.model as string;
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir: string | undefined =
2023-09-10 19:42:18 +02:00
folderPath || (payload.outputPath as string);
2023-09-10 11:14:04 +02:00
2023-09-10 19:42:18 +02:00
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
2023-09-10 11:14:04 +02:00
}
const isDefaultModel = DEFAULT_MODELS.includes(model);
const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "") as string;
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
2023-09-18 20:30:43 +02:00
let initialScale = "4";
2023-09-10 11:14:04 +02:00
if (model.includes("x2")) {
2023-09-18 20:30:43 +02:00
initialScale = "2";
2023-09-10 11:14:04 +02:00
} else if (model.includes("x3")) {
2023-09-18 20:30:43 +02:00
initialScale = "3";
2023-09-10 11:14:04 +02:00
} else {
2023-09-18 20:30:43 +02:00
initialScale = "4";
2023-09-10 11:14:04 +02:00
}
2023-09-18 20:30:43 +02:00
const desiredScale = payload.scale;
2023-09-10 11:14:04 +02:00
const outFile =
outputDir +
slash +
fileName +
"_upscayl_" +
2023-09-19 16:51:38 +02:00
(noImageProcessing ? initialScale : desiredScale) +
2023-09-10 11:14:04 +02:00
"x_" +
model +
"." +
saveImageAs;
// UPSCALE
2023-09-10 19:54:08 +02:00
if (fs.existsSync(outFile) && !overwrite) {
2023-09-10 11:14:04 +02:00
// If already upscayled, just output that file
logit("✅ Already upscayled at: ", outFile);
mainWindow.webContents.send(
COMMAND.UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0])
)
);
} else {
2023-09-18 20:30:43 +02:00
logit("✅ Upscayl Variables: ", {
model,
gpuId,
saveImageAs,
inputDir,
outputDir,
fullfileName,
fileName,
initialScale: initialScale,
desiredScale,
outFile,
compression,
});
2023-09-10 11:14:04 +02:00
const upscayl = spawnUpscayl(
"realesrgan",
getSingleImageArguments(
inputDir,
fullfileName,
2023-10-15 11:37:04 +02:00
outFile.slice(0, -3) + "png",
2023-09-10 19:42:18 +02:00
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
2023-09-10 11:14:04 +02:00
model,
2023-09-18 20:30:43 +02:00
initialScale,
2023-09-10 11:14:04 +02:00
gpuId,
"png"
),
logit
);
setChildProcesses(upscayl);
2023-09-10 19:42:18 +02:00
setStopped(false);
2023-09-10 11:14:04 +02:00
let isAlpha = false;
let failed = false;
const onData = (data: string) => {
logit("image upscayl: ", data.toString());
mainWindow.setProgressBar(parseFloat(data.slice(0, data.length)) / 100);
data = data.toString();
mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());
if (data.includes("invalid gpu") || data.includes("failed")) {
logit("❌ INVALID GPU OR FAILED");
upscayl.kill();
failed = true;
}
if (data.includes("has alpha channel")) {
logit("📢 INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
isAlpha = true;
}
};
const onError = (data) => {
if (!mainWindow) return;
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());
failed = true;
upscayl.kill();
return;
};
const onClose = async () => {
2023-09-10 19:42:18 +02:00
if (!failed && !stopped) {
2023-09-10 11:14:04 +02:00
logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
2023-09-19 17:04:31 +02:00
if (noImageProcessing) {
2023-09-18 20:30:43 +02:00
logit("🚫 Skipping scaling and converting");
2023-09-10 11:14:04 +02:00
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0])
)
);
2023-09-19 17:14:15 +02:00
return;
2023-09-10 11:14:04 +02:00
}
2023-09-19 17:04:31 +02:00
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
// Free up memory
upscayl.kill();
try {
await convertAndScale(
inputDir + slash + fullfileName,
2023-10-15 11:37:04 +02:00
outFile.slice(0, -3) + "png",
2023-09-19 17:04:31 +02:00
outFile,
desiredScale,
saveImageAs,
onError
);
2023-10-14 10:49:57 +02:00
// Remove the png file (default) if the saveImageAs is not png
2023-10-15 11:37:04 +02:00
fs.access(outFile.slice(0, -3) + "png", fs.constants.F_OK, (err) => {
if (!err && saveImageAs !== "png") {
logit("🗑 Removing png file");
fs.unlinkSync(outFile.slice(0, -3) + "png");
}
});
2023-09-19 17:04:31 +02:00
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0])
)
);
} catch (error) {
logit(
"❌ Error processing (scaling and converting) the image. Please report this error on GitHub.",
error
);
upscayl.kill();
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
2023-10-14 10:41:57 +02:00
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
error
2023-09-19 17:04:31 +02:00
);
}
2023-09-10 11:14:04 +02:00
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
}
};
export default imageUpscayl;