From 9be3c33d2672e3a0687d27b321f5899eef24da1d Mon Sep 17 00:00:00 2001 From: lanyeeee <1210347077@qq.com> Date: Wed, 8 Jan 2025 06:08:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=97=A0=E6=B3=95=E5=8E=BB=E6=B0=B4?= =?UTF-8?q?=E5=8D=B0=E7=9A=84=E5=9B=BE=E7=89=87=E5=B0=86=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E6=96=B0=E5=BB=BA=E5=9B=BE=E7=89=87=E9=87=8D=E7=BB=98=EF=BC=8C?= =?UTF-8?q?=E8=80=8C=E6=98=AF=E7=9B=B4=E6=8E=A5=E5=A4=8D=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/commands/remove_watermark.rs | 28 +++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src-tauri/src/commands/remove_watermark.rs b/src-tauri/src/commands/remove_watermark.rs index 350d118..e7c7a72 100644 --- a/src-tauri/src/commands/remove_watermark.rs +++ b/src-tauri/src/commands/remove_watermark.rs @@ -55,18 +55,28 @@ pub fn remove_watermark( ))?; // 构建输出图片的路径(输出目录/漫画名/章节名/图片名) let out_image_path = output_dir.join(relative_path); - // 打开输入图片 - let mut img = image::open(img_path) - .context(format!("打开图片 {img_path:?} 失败"))? - .to_rgb8(); - let (width, height) = (img.width(), img.height()); + // 获取图片的尺寸 + let (width, height) = image::image_dimensions(img_path) + .context(format!("获取图片 {img_path:?} 的尺寸失败"))?; if let Some((black, white)) = backgrounds.get(&(width, height)) { - // 只有在backgrounds中找到了黑色背景和白色背景的水印图片才会去除水印 + // 在backgrounds中找到了黑色背景和白色背景的水印图片,可以去除水印 + let mut img = image::open(img_path) + .context(format!("打开图片 {img_path:?} 失败"))? + .to_rgb8(); + remove_image_watermark(black, white, &mut img); + + save_image(&img, &out_image_path, &format, optimize) + .context(format!("保存图片 {out_image_path:?} 失败"))?; + } else { + // 否则,直接复制图片到输出目录 + if let Some(parent) = out_image_path.parent() { + // 保证输出目录存在 + std::fs::create_dir_all(parent).context(format!("创建目录 {parent:?} 失败"))?; + } + std::fs::copy(img_path, &out_image_path) + .context(format!("复制图片 {img_path:?} 到 {out_image_path:?} 失败"))?; } - // 保存去除水印后的图片(无论是否成功去除水印都会保存) - save_image(&img, &out_image_path, &format, optimize) - .context(format!("保存图片 {out_image_path:?} 失败"))?; // 更新目录的进度 let (current, total) = { let mut dir_progress = dir_progress.lock();