From 0541dbfd696dafc7212c9058910c89bbc05295d3 Mon Sep 17 00:00:00 2001 From: Julian Freeman Date: Tue, 20 Jan 2026 22:08:56 -0400 Subject: [PATCH] fix image type bug --- README.md | 4 ++-- package.json | 2 +- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 4 ++-- src-tauri/src/lib.rs | 26 ++++++++++++++++++-------- src-tauri/tauri.conf.json | 4 ++-- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index cbe98f8..9fc9cb5 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,5 @@ Generated by Gemini Models: -LaMa: https://huggingface.co/Carve/LaMa-ONNX/blob/main/lama_fp32.onnx -en_PP-OCRv3_det_infer: https://huggingface.co/SWHL/RapidOCR/blob/main/PP-OCRv4/en_PP-OCRv3_det_infer.onnx +- LaMa: https://huggingface.co/Carve/LaMa-ONNX/blob/main/lama_fp32.onnx +- en_PP-OCRv3_det_infer: https://huggingface.co/SWHL/RapidOCR/blob/main/PP-OCRv4/en_PP-OCRv3_det_infer.onnx diff --git a/package.json b/package.json index ed48ff5..f8c38aa 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "watermark-wizard", "private": true, - "version": "1.0.0", + "version": "1.0.1", "type": "module", "scripts": { "dev": "vite", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index e807a10..d2c24e4 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -5117,7 +5117,7 @@ dependencies = [ [[package]] name = "watermark-wizard" -version = "1.0.0" +version = "1.0.1" dependencies = [ "ab_glyph", "image", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 71d8061..0b8a407 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "watermark-wizard" -version = "1.0.0" +version = "1.0.1" description = "A Tauri App handles watermarks" -authors = ["you"] +authors = ["Julian"] edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 1ae3ba8..ca41855 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -19,6 +19,14 @@ fn get_cache_dir() -> std::path::PathBuf { path } +fn load_image_safe(path: &Path) -> Result { + let bytes = fs::read(path).map_err(|e| { + let exists = path.exists(); + format!("读取文件失败 (存在: {}) / Failed to read file '{}': {}", exists, path.to_string_lossy(), e) + })?; + image::load_from_memory(&bytes).map_err(|e| format!("图片解码失败 / Failed to decode image '{}': {}", path.to_string_lossy(), e)) +} + fn generate_thumbnail(original_path: &Path) -> Option { let cache_dir = get_cache_dir(); @@ -35,7 +43,7 @@ fn generate_thumbnail(original_path: &Path) -> Option { } // Generate - if let Ok(img) = image::open(original_path) { + if let Ok(img) = load_image_safe(original_path) { let thumb = img.thumbnail(u32::MAX, 200); let _file = fs::File::create(&thumb_path).ok()?; thumb.save_with_format(&thumb_path, image::ImageFormat::Jpeg).ok()?; @@ -153,9 +161,11 @@ async fn export_batch(images: Vec, watermark: WatermarkSettings let results: Vec> = images.par_iter().map(|task| { let input_path = Path::new(&task.path); - let img_result = image::open(input_path); - if let Ok(dynamic_img) = img_result { + // Use safe loading helper + let img_result = load_image_safe(input_path); + + if let Ok(dynamic_img) = &img_result { let mut base_img = dynamic_img.to_rgba8(); let (width, height) = base_img.dimensions(); @@ -277,7 +287,7 @@ async fn export_batch(images: Vec, watermark: WatermarkSettings Ok(()) } else { - Err(format!("Failed to open {}", task.path)) + Err(img_result.unwrap_err()) } }).collect(); @@ -386,7 +396,7 @@ fn calculate_zca_internal(img: &image::DynamicImage) -> Result Result { - let img = image::open(&path).map_err(|e| e.to_string())?; + let img = load_image_safe(Path::new(&path))?; calculate_zca_internal(&img) } @@ -399,7 +409,7 @@ struct LayoutResult { #[tauri::command] async fn layout_watermark(path: String, text: String, base_scale: f64) -> Result { - let img = image::open(&path).map_err(|e| e.to_string())?; + let img = load_image_safe(Path::new(&path))?; let (width, height) = img.dimensions(); let font = FontRef::try_from_slice(FONT_DATA).map_err(|e| format!("Font error: {}", e))?; @@ -468,7 +478,7 @@ struct Rect { #[tauri::command] async fn detect_watermark(app: AppHandle, path: String) -> Result { - let img = image::open(&path).map_err(|e| e.to_string())?.to_rgba8(); + let img = load_image_safe(Path::new(&path))?.to_rgba8(); // 1. Try OCR Detection let ocr_model_path = app.path().resource_dir() @@ -662,7 +672,7 @@ enum MaskStroke { #[tauri::command] async fn run_inpainting(app: AppHandle, path: String, strokes: Vec) -> Result { - let img = image::open(&path).map_err(|e| e.to_string())?.to_rgba8(); + let img = load_image_safe(Path::new(&path))?.to_rgba8(); let (width, height) = img.dimensions(); // 1. Create Gray Mask (0 = keep, 255 = remove) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 61d6a6f..ec3dda4 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "watermark-wizard", - "version": "1.0.0", + "version": "1.0.1", "identifier": "top.volan.watermark-wizard", "build": { "beforeDevCommand": "pnpm dev", @@ -12,7 +12,7 @@ "app": { "windows": [ { - "title": "水印精灵 v1.0.0", + "title": "水印精灵 v1.0.1", "width": 1650, "height": 1000 }