Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: 无法去水印的图片将不再新建图片重绘,而是直接复制 #60

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src-tauri/src/commands/remove_watermark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down