Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve original filename in single-image Upscayl #966

Open
2 tasks done
Nero1024x opened this issue Sep 8, 2024 · 8 comments
Open
2 tasks done

Preserve original filename in single-image Upscayl #966

Nero1024x opened this issue Sep 8, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@Nero1024x
Copy link

Nero1024x commented Sep 8, 2024

Checklist

  • I have checked that this issue isn't addressed in the FAQ.
  • I have searched for this in the issues tab.

Description

Can we have option not to rename files, but leave output names as is? And I have no knowledge of coding. Thank you

Related Problem(s)

No response

Alternatives

No response

@Nero1024x Nero1024x added the enhancement New feature or request label Sep 8, 2024
Copy link

dosubot bot commented Sep 8, 2024

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

Yes, you can have an option not to rename files and leave output names as is. This would require modifying the imageUpscayl function to include a new parameter, keepOriginalName. Here is an example of how this can be implemented:

  1. Modify the imageUpscayl function:
const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
  const mainWindow = getMainWindow();

  if (!mainWindow) {
    logit("No main window found");
    return;
  }

  // GET VARIABLES
  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 as string;
  const gpuId = payload.gpuId as string;
  const saveImageAs = payload.saveImageAs as ImageFormat;
  const overwrite = payload.overwrite as boolean;
  const imagePath = decodePath(payload.imagePath);
  const keepOriginalName = payload.keepOriginalName as boolean; // New parameter
  let inputDir = getDirectoryFromPath(imagePath);
  let outputDir = decodePath(payload.outputPath);
  const fileNameWithExt = getFilenameFromPath(imagePath);
  const fileName = parse(fileNameWithExt).name;

  const outFile = keepOriginalName
    ? outputDir + slash + fileNameWithExt
    : outputDir +
      slash +
      fileName +
      "_upscayl_" +
      (useCustomWidth ? `${customWidth}px_` : `${scale}x_`) +
      model +
      "." +
      saveImageAs;

  const isDefaultModel = DEFAULT_MODELS.includes(model);

  // Check if windows can write the new filename to the file system
  if (outFile.length >= 255) {
    logit("Filename too long for Windows.");
    mainWindow.webContents.send(COMMAND.UPSCAYL_ERROR, "The filename exceeds the maximum path length allowed by Windows. Please shorten the filename or choose a different save location.");
  }

  // UPSCALE
  if (fs.existsSync(outFile) && !overwrite) {
    // If already upscayled, just output that file
    logit("✅ Already upscayled at: ", outFile);
    mainWindow.webContents.send(COMMAND.UPSCAYL_DONE, outFile);
  } else {
    logit(
      "✅ Upscayl Variables: ",
      JSON.stringify({
        model,
        gpuId,
        saveImageAs,
        inputDir,
        fileNameWithExt,
        outputDir,
        outFile,
        fileName,
        scale,
        compression,
        customWidth,
        useCustomWidth,
        tileSize,
      }),
    );
    const upscayl = spawnUpscayl(
      getSingleImageArguments({
        inputDir: decodeURIComponent(inputDir),
        fileNameWithExt: decodeURIComponent(fileNameWithExt),
        outFile,
        modelsPath: isDefaultModel
          ? modelsPath
          : savedCustomModelsPath ?? modelsPath,
        model,
        scale,
        gpuId,
        saveImageAs,
        customWidth,
        compression,
        tileSize,
      }),
      logit,
    );

    setChildProcesses(upscayl);

    setStopped(false);
    let failed = false;
    const onData = (data: string) => {
      logit(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("Error")) {
        upscayl.kill();
        failed = true;
      } else if (data.includes("Resizing")) {
        mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
      }
    };
    const onError = (data) => {
      if (!mainWindow) return;
      mainWindow.setProgressBar(-1);
      mainWindow.webContents.send(COMMAND.UPSCAYL_ERROR, data.toString());
      failed = true;
      upscayl.kill();
      return;
    };
    const onClose = async () => {
      if (!failed && !stopped) {
        logit("💯 Done upscaling");
        // Free up memory
        upscayl.kill();
        mainWindow.setProgressBar(-1);
        mainWindow.webContents.send(COMMAND.UPSCAYL_DONE, outFile);
        showNotification("Upscayl", "Image upscayled successfully!");
      }
    };

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

export default imageUpscayl;
  1. Update the ImageUpscaylPayload type:

Add the keepOriginalName property to the ImageUpscaylPayload type definition in common/types/types.d.ts:

export type ImageUpscaylPayload = {
  imagePath: string;
  outputPath: string;
  scale: string;
  model: string;
  gpuId: string;
  saveImageAs: ImageFormat;
  overwrite: boolean;
  compression: string;
  noImageProcessing: boolean;
  customWidth: string;
  useCustomWidth: boolean;
  tileSize: number;
  keepOriginalName: boolean; // New property
};
  1. Add a setting for keepOriginalName:

Update the settings UI to include a toggle for keepOriginalName in renderer/components/settings-tab/index.tsx:

import { useState } from "react";

// Add this state to manage the keepOriginalName setting
const [keepOriginalName, setKeepOriginalName] = useState(
  localStorage.getItem("keepOriginalName") === "true"
);

// Add this handler to update the setting
const handleKeepOriginalNameChange = () => {
  const newValue = !keepOriginalName;
  setKeepOriginalName(newValue);
  localStorage.setItem("keepOriginalName", newValue.toString());
};

// Add this toggle in the return statement of the SettingsTab component
<OverwriteToggle />
<TurnOffNotificationsToggle />

{/* KEEP ORIGINAL NAME TOGGLE */}
<div className="flex items-center">
  <label htmlFor="keepOriginalName" className="mr-2">
    {t("SETTINGS.KEEP_ORIGINAL_NAME.TITLE")}
  </label>
  <input
    type="checkbox"
    id="keepOriginalName"
    checked={keepOriginalName}
    onChange={handleKeepOriginalNameChange}
  />
</div>
  1. Update localization files:

Add the new setting to the localization files, such as renderer/locales/en.json:

"SETTINGS": {
  "TITLE": "SETTINGS",
  ...
  "KEEP_ORIGINAL_NAME": {
    "TITLE": "KEEP ORIGINAL NAME",
    "DESCRIPTION": "If enabled, Upscayl will keep the original name of the image file."
  },
  ...
}

This will allow users to enable or disable the renaming of files through the settings UI [1][2][3][4].

To continue talking to Dosu, mention @dosu.

@abhishek-gaonkar
Copy link
Collaborator

Hi @Nero1024x
I am slightly confused by your question. Did you want the output file to have the same name as the input file?
If yes, then that's not possible since you cannot have two files with the same name inside a folder.
If you meant something else, please do elaborate

@Nero1024x
Copy link
Author

@abhishek-gaonkar Yes. Option to keep the same name as input files, but when output is different folder than input folder

@abhishek-gaonkar
Copy link
Collaborator

Understood.. @NayamAmarshe shall I take this?

@NayamAmarshe
Copy link
Member

Understood.. @NayamAmarshe shall I take this?

We're already doing this for batch upscayl. The file names will have the same names.

Copy link

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.

@github-actions github-actions bot added the stale We lack your response. Please reply to re-start the issue (ignore if added by Dosu) label Oct 11, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2024
@aaronliu0130 aaronliu0130 reopened this Nov 1, 2024
@aaronliu0130 aaronliu0130 removed the stale We lack your response. Please reply to re-start the issue (ignore if added by Dosu) label Nov 1, 2024
@aaronliu0130 aaronliu0130 moved this to In Progress in Upscayl Development Nov 1, 2024
@aaronliu0130 aaronliu0130 mentioned this issue Nov 1, 2024
2 tasks
@aaronliu0130 aaronliu0130 changed the title Renaming Preserve original filename in single-image Upscayl Nov 1, 2024
Copy link

github-actions bot commented Dec 2, 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.

@github-actions github-actions bot added the stale We lack your response. Please reply to re-start the issue (ignore if added by Dosu) label Dec 2, 2024
@aaronliu0130 aaronliu0130 removed the stale We lack your response. Please reply to re-start the issue (ignore if added by Dosu) label Dec 2, 2024
Copy link

github-actions bot commented Jan 2, 2025

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.

@github-actions github-actions bot added the stale We lack your response. Please reply to re-start the issue (ignore if added by Dosu) label Jan 2, 2025
@aaronliu0130 aaronliu0130 removed the stale We lack your response. Please reply to re-start the issue (ignore if added by Dosu) label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In Progress
Development

No branches or pull requests

4 participants