2023-09-10 11:14:04 +02:00
|
|
|
import sharp from "sharp";
|
|
|
|
import logit from "./logit";
|
2023-09-10 19:42:18 +02:00
|
|
|
import { getMainWindow } from "../main-window";
|
|
|
|
import { quality } from "./config-variables";
|
2023-09-10 11:14:04 +02:00
|
|
|
|
|
|
|
const convertAndScale = async (
|
|
|
|
originalImagePath: string,
|
|
|
|
upscaledImagePath: string,
|
|
|
|
processedImagePath: string,
|
|
|
|
scale: string,
|
|
|
|
saveImageAs: string,
|
|
|
|
onError: (error: any) => void
|
|
|
|
) => {
|
2023-09-10 19:42:18 +02:00
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
|
2023-09-10 11:14:04 +02:00
|
|
|
const originalImage = await sharp(originalImagePath).metadata();
|
|
|
|
if (!mainWindow || !originalImage) {
|
|
|
|
throw new Error("Could not grab the original image!");
|
|
|
|
}
|
|
|
|
// Resize the image to the scale
|
|
|
|
const newImage = sharp(upscaledImagePath)
|
|
|
|
.resize(
|
|
|
|
originalImage.width && originalImage.width * parseInt(scale),
|
|
|
|
originalImage.height && originalImage.height * parseInt(scale)
|
|
|
|
)
|
|
|
|
.withMetadata(); // Keep metadata
|
|
|
|
// Change the output according to the saveImageAs
|
|
|
|
if (saveImageAs === "png") {
|
2023-09-10 19:42:18 +02:00
|
|
|
newImage.png({ quality: 100 - quality });
|
2023-09-10 11:14:04 +02:00
|
|
|
} else if (saveImageAs === "jpg") {
|
2023-09-10 19:42:18 +02:00
|
|
|
console.log("Quality: ", quality);
|
|
|
|
newImage.jpeg({ quality: 100 - quality });
|
2023-09-10 11:14:04 +02:00
|
|
|
}
|
|
|
|
// Save the image
|
|
|
|
const buffer = await newImage.toBuffer();
|
|
|
|
sharp(buffer)
|
|
|
|
.toFile(processedImagePath)
|
|
|
|
.then(() => {
|
|
|
|
logit("✅ Done converting to: ", upscaledImagePath);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
logit("❌ Error converting to: ", saveImageAs, error);
|
|
|
|
onError(error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default convertAndScale;
|