V 2.2.7 lama iopaint 去水印

This commit is contained in:
2024-06-06 13:12:04 +08:00
parent 8b011856c2
commit f27ec08724
39 changed files with 2495 additions and 547 deletions
+4
View File
@@ -0,0 +1,4 @@
import fs from 'node:fs';
const fspromises = fs.promises;
+105
View File
@@ -0,0 +1,105 @@
import fs from "fs"
import path from "path";
const fspromises = fs.promises;
/**
* 判断文件或目录是否存在
* @param {*} path 文件或目录的路径
* @returns true表示存在,false表示不存在
*/
export async function CheckFileOrDirExist(path) {
try {
await fspromises.access(path);
return true; // 文件或目录存在
} catch (error) {
return false; // 文件或目录不存在
}
}
/** * 判断一个文件地址是不是文件夹
* @param {*} path 输入的文件地址
* @returns true 是 false 不是
*/
export async function IsDirectory(path) {
try {
const stat = await fspromises.stat(path);
return stat.isDirectory();
} catch (error) {
throw new Error(`获取文件夹信息失败: ${path}`);
}
}
/**
* 将文件或者是文件夹备份到指定的文职
* @param {*} source_path 源文件/文件夹地址
* @param {*} target_path 目标文件/文件夹地址
*/
export async function BackupFileOrFolder(source_path, target_path) {
try {
// 判断父文件夹是否存在,不存在创建
const parent_path = path.dirname(target_path);
if (!(await CheckFileOrDirExist(parent_path))) {
await fspromises.mkdir(parent_path, { recursive: true });
}
// 判断是不是文件夹
const isDirectory = await IsDirectory(source_path);
if (isDirectory) {
// 复制文件夹
await fspromises.rename(source_path, target_path);
} else {
// 复制文件
await fspromises.copyFile(source, target);
}
} catch (error) {
throw new Error(error);
}
}
/**
* 获取指定的文件夹下面的所有的指定的拓展名的文件
* @param {*} folderPath 文件夹地址
* @param {*} extensions 拓展地址
* @returns 返回文件中指定的后缀文件地址(绝对地址)
*/
export async function GetFilesWithExtensions(folderPath, extensions) {
try {
// 判断当前是不是文件夹
if (!(await IsDirectory(folderPath))) {
throw new Error("输入的不是有效的文件夹地址")
}
let entries = await fspromises.readdir(folderPath, { withFileTypes: true });
let files = [];
// 使用Promise.all来并行处理所有的stat调用
const fileStats = await Promise.all(entries.map(async (entry) => {
const entryPath = path.join(folderPath, entry.name);
if (entry.isFile()) {
return {
name: entry.name,
path: entryPath,
isFile: true,
};
} else {
return {
isFile: false,
};
}
}));
// 过滤出文件并且满足扩展名要求的文件
files = fileStats.filter(fileStat => fileStat.isFile && extensions.includes(path.extname(fileStat.name).toLowerCase()));
// 对files数组进行排序,基于文件名
files.sort((a, b) => a.name.localeCompare(b.name));
// 返回文件名数组(完整的)
return files.map(fileStat => path.join(folderPath, fileStat.name));
} catch (error) {
throw new Error(error);
}
}
+72
View File
@@ -0,0 +1,72 @@
import sharp from "sharp";
import { CheckFileOrDirExist } from "./file"
/**
* 将指定的图片的尺寸修改,返回修改后的图片数据(base64或buffer)
* @param {*} image_path
* @param {*} width
* @param {*} height
* @param {*} type
* @returns 返回修改后的图片数据(base64或buffer)
*/
export async function ResizeImage(image_path, width, height, type) {
try {
// 检查 type 参数
if (type !== 'base64' && type !== 'buffer') {
throw new Error('type 参数必须是 "base64" 或 "buffer"');
}
// 判断是不是图片文件
if (!image_path.match(/\.(jpg|jpeg|png)$/)) {
throw new Error("输入的文件地址不是图片文件地址");
}
// 判断文件是否存在
if (!(await CheckFileOrDirExist(image_path))) {
throw new Error("文件不存在");
}
// 修改图片尺寸
const image = sharp(image_path);
image.resize(width, height);
let data = await image.toBuffer();
if (type === 'base64') {
return data.toString('base64');
} else {
return data;
}
} catch (error) {
throw error;
}
}
/**
* 获取指定图片文件的宽高
* @param {*} image_path 图片文件的路径
* @returns 返回以一个对象,包含width和height属性
*/
export async function GetImageSize(image_path) {
try {
// 判断文件是否存在
if (!(await CheckFileOrDirExist(image_path))) {
throw new Error("文件不存在");
}
// 判断是不是图片文件
if (!image_path.match(/\.(jpg|jpeg|png)$/)) {
throw new Error("输入的文件地址不是图片文件地址");
}
// 获取图片的宽高
const metadata = await sharp(image_path).metadata();
return {
width: metadata.width,
height: metadata.height
}
} catch (error) {
throw error;
}
}
+9
View File
@@ -0,0 +1,9 @@
import * as image from './image';
import * as common from './common';
import * as file from './file'
export {
image,
common,
file
};
+2
View File
@@ -13,6 +13,7 @@ if (!app.isPackaged) {
img_base: path.join(__dirname, "../../resources/config/img_base.json"),
video_config: path.join(__dirname, "../../resources/config/video_config.json"),
scripts_path: path.join(__dirname, "../../resources/scripts"),
logger_path: path.join(__dirname, "../../resources/logger"),
package_path: path.join(__dirname, "../../resources/package"),
image_path: path.join(__dirname, "../../resources/image"),
temp_sd_image: path.join(__dirname, "../../resources/image/TempSDImage"),
@@ -43,6 +44,7 @@ if (!app.isPackaged) {
video_config: path.join(__dirname, "../../../resources/config/video_config.json"),
img_base: path.join(__dirname, "../../../resources/config/img_base.json"),
scripts_path: path.join(__dirname, "../../../resources/scripts"),
logger_path: path.join(__dirname, "../../../resources/logger"),
package_path: path.join(__dirname, "../../../resources/package"),
discordScript: path.join(__dirname, "../../../resources/scripts/discordScript.js"),
image_path: path.join(__dirname, "../../../resources/image"),
+8 -1
View File
@@ -180,6 +180,13 @@ export const DEFINE_STRING = {
OPEN_DISCORD_WINDOW: "OPEN_DISCORD_WINDOW"
},
IMG: {
ONE_SPLIT_FOUR: "ONE_SPLIT_FOUR"
ONE_SPLIT_FOUR: "ONE_SPLIT_FOUR",
BASE64_TO_FILE: "BASE64_TO_FILE",
PROCESS_IMAGE: "PROCESS_IMAGE",
BATCH_PROCESS_IMAGE: "BATCH_PROCESS_IMAGE",
BATCH_PROCESS_IMAGE_RESULT: "BATCH_PROCESS_IMAGE_RESULT"
},
SYSTEM: {
OPEN_FILE: "OPEN_FILE",
}
}
+3
View File
@@ -0,0 +1,3 @@
export const LOGGER_DEFINE = {
REMOVE_WATERMARK: "去除水印",
}