Coder Social home page Coder Social logo

Output file name unchanged? about upscayl HOT 5 CLOSED

Softboi1 avatar Softboi1 commented on September 13, 2024 1
Output file name unchanged?

from upscayl.

Comments (5)

dosubot avatar dosubot commented on September 13, 2024 2

Hey @Softboi1! 👋 I'm here to help you with any bugs, questions, or contributions you might have. Let's squash those pesky bugs together!

To upscale a large batch of files without changing their names and replace the old versions with the new upscaled ones, you can modify the batchUpscayl function to save the upscaled images directly to the input directory, effectively overwriting the original files. Here is the modified code:

import fs from "fs";
import { getMainWindow } from "../main-window";
import {
  childProcesses,
  savedCustomModelsPath,
  setStopped,
  stopped,
} from "../utils/config-variables";
import logit from "../utils/logit";
import { spawnUpscayl } from "../utils/spawn-upscayl";
import { getBatchArguments } from "../utils/get-arguments";
import slash from "../utils/slash";
import { modelsPath } from "../utils/get-resource-paths";
import COMMAND from "../../common/commands";
import { BatchUpscaylPayload } from "../../common/types/types";
import showNotification from "../utils/show-notification";
import { DEFAULT_MODELS } from "../../common/models-list";

const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
  const mainWindow = getMainWindow();
  if (!mainWindow) return;

  const tileSize = payload.tileSize;
  const compression = payload.compression;
  const scale = payload.scale;
  const useCustomWidth = payload.useCustomWidth;
  const customWidth = useCustomWidth ? payload.customWidth : "";
  const model = payload.model;
  const gpuId = payload.gpuId;
  const saveImageAs = payload.saveImageAs;
  // GET THE IMAGE DIRECTORY
  let inputDir = decodeURIComponent(payload.batchFolderPath);
  // SET THE OUTPUT DIRECTORY TO THE INPUT DIRECTORY
  let outputFolderPath = inputDir;

  const isDefaultModel = DEFAULT_MODELS.includes(model);

  // UPSCALE
  const upscayl = spawnUpscayl(
    getBatchArguments({
      inputDir,
      outputDir: outputFolderPath,
      modelsPath: isDefaultModel
        ? modelsPath
        : savedCustomModelsPath ?? modelsPath,
      model,
      gpuId,
      saveImageAs,
      scale,
      customWidth,
      compression,
      tileSize,
    }),
    logit,
  );

  childProcesses.push(upscayl);

  setStopped(false);
  let failed = false;
  let encounteredError = false;

  const onData = (data: any) => {
    if (!mainWindow) return;
    data = data.toString();
    mainWindow.webContents.send(
      COMMAND.FOLDER_UPSCAYL_PROGRESS,
      data.toString(),
    );
    if ((data as string).includes("Error")) {
      logit("❌ ", data);
      encounteredError = true;
    } else if (data.includes("Resizing")) {
      mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
    }
  };
  const onError = (data: any) => {
    if (!mainWindow) return;
    mainWindow.setProgressBar(-1);
    mainWindow.webContents.send(
      COMMAND.FOLDER_UPSCAYL_PROGRESS,
      data.toString(),
    );
    failed = true;
    upscayl.kill();
    mainWindow &&
      mainWindow.webContents.send(
        COMMAND.UPSCAYL_ERROR,
        `Error upscaling images! ${data}`,
      );
    return;
  };
  const onClose = () => {
    if (!mainWindow) return;
    if (!failed && !stopped) {
      logit("💯 Done upscaling");
      upscayl.kill();
      mainWindow.webContents.send(
        COMMAND.FOLDER_UPSCAYL_DONE,
        outputFolderPath,
      );
      if (!encounteredError) {
        showNotification("Upscayled", "Images upscayled successfully!");
      } else {
        showNotification(
          "Upscayled",
          "Images were upscayled but encountered some errors!",
        );
      }
    } else {
      upscayl.kill();
    }
  };
  upscayl.process.stderr.on("data", onData);
  upscayl.process.on("error", onError);
  upscayl.process.on("close", onClose);
};

export default batchUpscayl;

In this modified version, the outputFolderPath is set to the inputDir, which means the upscaled images will be saved in the same directory as the original images, effectively replacing them [1].

To continue talking to Dosu, mention @dosu.

from upscayl.

dosubot avatar dosubot commented on September 13, 2024 2

To modify your specific code to upscale a large batch of files without changing their names and to replace the old versions with the new upscaled ones, you need to make the following changes:

  1. Remove the creation of a new output directory.
  2. Set the output directory to be the same as the input directory.
  3. Ensure the upscaled images have the same filenames as the original images.

Here are the specific edits to your code:

var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const main_window_1 = require("../main-window");
const config_variables_1 = require("../utils/config-variables");
const logit_1 = __importDefault(require("../utils/logit"));
const spawn_upscayl_1 = require("../utils/spawn-upscayl");
const get_arguments_1 = require("../utils/get-arguments");
const slash_1 = __importDefault(require("../utils/slash"));
const get_resource_paths_1 = require("../utils/get-resource-paths");
const commands_1 = __importDefault(require("../../common/commands"));
const show_notification_1 = __importDefault(require("../utils/show-notification"));
const models_list_1 = require("../../common/models-list");

const batchUpscayl = (event, payload) => __awaiter(void 0, void 0, void 0, function* () {
    const mainWindow = (0, main_window_1.getMainWindow)();
    if (!mainWindow) return;

    const tileSize = payload.tileSize;
    const compression = payload.compression;
    const scale = payload.scale;
    const useCustomWidth = payload.useCustomWidth;
    const customWidth = useCustomWidth ? payload.customWidth : "";
    const model = payload.model;
    const gpuId = payload.gpuId;
    const saveImageAs = payload.saveImageAs;

    // GET THE IMAGE DIRECTORY
    let inputDir = decodeURIComponent(payload.batchFolderPath);
    // SET THE OUTPUT DIRECTORY TO BE THE SAME AS THE INPUT DIRECTORY
    let outputFolderPath = inputDir;

    const isDefaultModel = models_list_1.DEFAULT_MODELS.includes(model);

    // UPSCALE
    const upscayl = (0, spawn_upscayl_1.spawnUpscayl)((0, get_arguments_1.getBatchArguments)({
        inputDir,
        outputDir: outputFolderPath,
        modelsPath: isDefaultModel
            ? get_resource_paths_1.modelsPath
            : config_variables_1.savedCustomModelsPath !== null && config_variables_1.savedCustomModelsPath !== void 0 ? config_variables_1.savedCustomModelsPath : get_resource_paths_1.modelsPath,
        model,
        gpuId,
        saveImageAs,
        scale,
        customWidth,
        compression,
        tileSize,
    }), logit_1.default);

    config_variables_1.childProcesses.push(upscayl);
    (0, config_variables_1.setStopped)(false);
    let failed = false;
    let encounteredError = false;

    const onData = (data) => {
        if (!mainWindow) return;
        data = data.toString();
        mainWindow.webContents.send(commands_1.default.FOLDER_UPSCAYL_PROGRESS, data.toString());
        if (data.includes("Error")) {
            (0, logit_1.default)("❌ ", data);
            encounteredError = true;
        } else if (data.includes("Resizing")) {
            mainWindow.webContents.send(commands_1.default.SCALING_AND_CONVERTING);
        }
    };

    const onError = (data) => {
        if (!mainWindow) return;
        mainWindow.setProgressBar(-1);
        mainWindow.webContents.send(commands_1.default.FOLDER_UPSCAYL_PROGRESS, data.toString());
        failed = true;
        upscayl.kill();
        mainWindow &&
            mainWindow.webContents.send(commands_1.default.UPSCAYL_ERROR, `Error upscaling images! ${data}`);
        return;
    };

    const onClose = () => {
        if (!mainWindow) return;
        if (!failed && !config_variables_1.stopped) {
            (0, logit_1.default)("💯 Done upscaling");
            upscayl.kill();
            mainWindow.webContents.send(commands_1.default.FOLDER_UPSCAYL_DONE, outputFolderPath);
            if (!encounteredError) {
                (0, show_notification_1.default)("Upscayled", "Images upscayled successfully!");
            } else {
                (0, show_notification_1.default)("Upscayled", "Images were upscayled but encountered some errors!");
            }
        } else {
            upscayl.kill();
        }
    };

    upscayl.process.stderr.on("data", onData);
    upscayl.process.on("error", onError);
    upscayl.process.on("close", onClose);
});

exports.default = batchUpscayl;

With these changes, the upscaled images will be saved in the same directory as the original images, effectively replacing the old versions with the new upscaled ones [1].

To continue talking to Dosu, mention @dosu.

from upscayl.

Softboi1 avatar Softboi1 commented on September 13, 2024 1

@dosu

the code you mentioned is not the same as mine... can you show me the edits i need to make to MY specific code?

here is my code

var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const main_window_1 = require("../main-window");
const config_variables_1 = require("../utils/config-variables");
const logit_1 = __importDefault(require("../utils/logit"));
const spawn_upscayl_1 = require("../utils/spawn-upscayl");
const get_arguments_1 = require("../utils/get-arguments");
const slash_1 = __importDefault(require("../utils/slash"));
const get_resource_paths_1 = require("../utils/get-resource-paths");
const commands_1 = __importDefault(require("../../common/commands"));
const show_notification_1 = __importDefault(require("../utils/show-notification"));
const models_list_1 = require("../../common/models-list");
const batchUpscayl = (event, payload) => __awaiter(void 0, void 0, void 0, function* () {
const mainWindow = (0, main_window_1.getMainWindow)();
if (!mainWindow)
return;
const tileSize = payload.tileSize;
const compression = payload.compression;
const scale = payload.scale;
const useCustomWidth = payload.useCustomWidth;
const customWidth = useCustomWidth ? payload.customWidth : "";
const model = payload.model;
const gpuId = payload.gpuId;
const saveImageAs = payload.saveImageAs;
// GET THE IMAGE DIRECTORY
let inputDir = decodeURIComponent(payload.batchFolderPath);
// GET THE OUTPUT DIRECTORY
let outputFolderPath = decodeURIComponent(payload.outputPath);
const outputFolderName = upscayl_${saveImageAs}_${model}_${useCustomWidth ? ${customWidth}px:${scale}x};
outputFolderPath += slash_1.default + outputFolderName;
// CREATE THE OUTPUT DIRECTORY
if (!fs_1.default.existsSync(outputFolderPath)) {
fs_1.default.mkdirSync(outputFolderPath, { recursive: true });
}
const isDefaultModel = models_list_1.DEFAULT_MODELS.includes(model);
// UPSCALE
const upscayl = (0, spawn_upscayl_1.spawnUpscayl)((0, get_arguments_1.getBatchArguments)({
inputDir,
outputDir: outputFolderPath,
modelsPath: isDefaultModel
? get_resource_paths_1.modelsPath
: config_variables_1.savedCustomModelsPath !== null && config_variables_1.savedCustomModelsPath !== void 0 ? config_variables_1.savedCustomModelsPath : get_resource_paths_1.modelsPath,
model,
gpuId,
saveImageAs,
scale,
customWidth,
compression,
tileSize,
}), logit_1.default);
config_variables_1.childProcesses.push(upscayl);
(0, config_variables_1.setStopped)(false);
let failed = false;
let encounteredError = false;
const onData = (data) => {
if (!mainWindow)
return;
data = data.toString();
mainWindow.webContents.send(commands_1.default.FOLDER_UPSCAYL_PROGRESS, data.toString());
if (data.includes("Error")) {
(0, logit_1.default)("� ", data);
encounteredError = true;
}
else if (data.includes("Resizing")) {
mainWindow.webContents.send(commands_1.default.SCALING_AND_CONVERTING);
}
};
const onError = (data) => {
if (!mainWindow)
return;
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(commands_1.default.FOLDER_UPSCAYL_PROGRESS, data.toString());
failed = true;
upscayl.kill();
mainWindow &&
mainWindow.webContents.send(commands_1.default.UPSCAYL_ERROR, Error upscaling images! ${data});
return;
};
const onClose = () => {
if (!mainWindow)
return;
if (!failed && !config_variables_1.stopped) {
(0, logit_1.default)("💯 Done upscaling");
upscayl.kill();
mainWindow.webContents.send(commands_1.default.FOLDER_UPSCAYL_DONE, outputFolderPath);
if (!encounteredError) {
(0, show_notification_1.default)("Upscayled", "Images upscayled successfully!");
}
else {
(0, show_notification_1.default)("Upscayled", "Images were upscayled but encountered some errors!");
}
}
else {
upscayl.kill();
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
});
exports.default = batchUpscayl;

from upscayl.

aaronliu0130 avatar aaronliu0130 commented on September 13, 2024 1

Another alternative that doesn't require JavaScript knowledge and completely averts all risk of AI channeling Their raw power through your precious computer and taking over the solar system is to post-process the file names.
I'd recommend PowerRename, which is part of Microsoft® (😱)'s PowerToys, must-had software for Windows. After installing PowerToys and finishing the batch upscayl, you select all the output inside the output folder with Ctrl+A after going inside the folder, right click, go under "More options" if you're using Windows 11, and click "Rename with PowerRename". Then, simply put the part of the filename you want to remove plus the file extension (e.g. _upscayl_4x_realesrgan-x4fast.jpg) into the "Search for" field, and only put the file extension (e.g. .jpg) into the "Replace with" field. Then, click "Apply".
If you're using Linux or macOS, you can ask dosu (or any AI (or Bing if you're lucky)) to find a command that renames stuff for you.

from upscayl.

github-actions avatar github-actions commented on September 13, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still happening, please reply to indicate so.

from upscayl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.