1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-28 01:10:52 +01:00

Fix batch upscale scale

This commit is contained in:
Nayam Amarshe 2023-08-11 15:15:27 +05:30
parent 72e2788a8b
commit ec3bb3aca3
3 changed files with 56 additions and 9 deletions

View File

@ -167,9 +167,9 @@ app.on("ready", async () => {
// GET OVERWRITE SETTINGS FROM LOCAL STORAGE // GET OVERWRITE SETTINGS FROM LOCAL STORAGE
mainWindow.webContents mainWindow.webContents
.executeJavaScript('localStorage.getItem("overwrite");', true) .executeJavaScript('localStorage.getItem("overwrite");', true)
.then((lastSavedQuality: string | null) => { .then((lastSavedOverwrite: boolean | null) => {
if (lastSavedQuality !== null) { if (lastSavedOverwrite !== null) {
quality = parseInt(lastSavedQuality); overwrite = lastSavedOverwrite;
} }
}); });
}); });
@ -511,7 +511,7 @@ ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
const model = payload.model; const model = payload.model;
const gpuId = payload.gpuId; const gpuId = payload.gpuId;
const saveImageAs = payload.saveImageAs; const saveImageAs = payload.saveImageAs;
const scale = payload.scale as string; // const scale = payload.scale as string;
// GET THE IMAGE DIRECTORY // GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath; let inputDir = payload.batchFolderPath;
@ -522,11 +522,29 @@ ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
outputDir = outputFolderPath; outputDir = outputFolderPath;
} }
const isDefaultModel = defaultModels.includes(model);
let scale = "4";
if (model.includes("x2")) {
scale = "2";
} else if (model.includes("x3")) {
scale = "3";
} else {
scale = "4";
}
outputDir += `_${model}_x${payload.scale}`;
if (!fs.existsSync(outputDir)) { if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true }); fs.mkdirSync(outputDir, { recursive: true });
} }
const isDefaultModel = defaultModels.includes(model); // Delete .DS_Store files
fs.readdirSync(inputDir).forEach((file) => {
if (file === ".DS_Store") {
logit("🗑️ Deleting .DS_Store file");
fs.unlinkSync(inputDir + slash + file);
}
});
// UPSCALE // UPSCALE
const upscayl = spawnUpscayl( const upscayl = spawnUpscayl(
@ -537,7 +555,7 @@ ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath, isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model, model,
gpuId, gpuId,
saveImageAs, "png",
scale scale
), ),
logit logit
@ -555,7 +573,7 @@ ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
commands.FOLDER_UPSCAYL_PROGRESS, commands.FOLDER_UPSCAYL_PROGRESS,
data.toString() data.toString()
); );
if (data.includes("invalid gpu") || data.includes("failed")) { if (data.includes("invalid") || data.includes("failed")) {
logit("❌ INVALID GPU OR INVALID FILES IN FOLDER - FAILED"); logit("❌ INVALID GPU OR INVALID FILES IN FOLDER - FAILED");
failed = true; failed = true;
upscayl.kill(); upscayl.kill();
@ -575,6 +593,28 @@ ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
if (!mainWindow) return; if (!mainWindow) return;
if (!failed && !stopped) { if (!failed && !stopped) {
logit("💯 Done upscaling"); logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
// Get number of files in output folder
const files = fs.readdirSync(inputDir);
files.forEach(async (file) => {
console.log("Filename: ", file.slice(0, -3));
// Resize the image to the original size
const originalImage = await Jimp.read(inputDir + slash + file);
const newImage = await Jimp.read(
outputDir + slash + file.slice(0, -3) + "png"
);
newImage
.quality(100 - quality)
.scaleToFit(
originalImage.getWidth() * parseInt(payload.scale),
originalImage.getHeight() * parseInt(payload.scale)
)
.write(outputDir + slash + file);
if (saveImageAs !== "png") {
fs.unlinkSync(outputDir + slash + file.slice(0, -3) + "png");
}
});
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir); mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
} else { } else {
upscayl.kill(); upscayl.kill();

View File

@ -1,4 +1,4 @@
import React from "react"; import React, { useEffect } from "react";
type ToggleOverwriteProps = { type ToggleOverwriteProps = {
overwrite: boolean; overwrite: boolean;
@ -6,6 +6,13 @@ type ToggleOverwriteProps = {
}; };
const ToggleOverwrite = ({ overwrite, setOverwrite }: ToggleOverwriteProps) => { const ToggleOverwrite = ({ overwrite, setOverwrite }: ToggleOverwriteProps) => {
useEffect(() => {
if (!localStorage.getItem("overwrite")) {
localStorage.setItem("overwrite", JSON.stringify(overwrite));
} else {
setOverwrite(localStorage.getItem("overwrite"));
}
}, []);
return ( return (
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-2">
<p className="text-sm font-medium">OVERWRITE PREVIOUS UPSCALE</p> <p className="text-sm font-medium">OVERWRITE PREVIOUS UPSCALE</p>

View File

@ -670,7 +670,7 @@ const Home = () => {
All done! All done!
</p> </p>
<button <button
className="bg-gradient-blue rounded-lg p-3 font-medium text-white/90 transition-colors" className="btn bg-gradient-blue rounded-lg p-3 font-medium text-white/90 transition-colors"
onClick={openFolderHandler}> onClick={openFolderHandler}>
Open Upscayled Folder Open Upscayled Folder
</button> </button>