tonhowtf/omniget · error

não gravei o JPEG

Error message

não gravei o JPEG {}: {}

What it means

`save_image` uses JpegEncoder to write JPEG output; if `enc.encode_image(&img.to_rgb8())` fails, it wraps the error as 'não gravei o JPEG {path}: {e}'. The file was created, but pixel encoding/writing into the BufWriter failed.

Solutions

  1. Check the wrapped inner error: if it's I/O, free disk space or fix permissions on the destination directory.
  2. Retry saving to a different local path (e.g. temp dir) to rule out mount/lock issues.
  3. Reduce image size or save as PNG (the tool's non-jpeg branch) if the JPEG encoder rejects the content.

Example fix

// before
let dest = Path::new("/mnt/network/out.jpg");
save_image(&img, dest, 90)?;
// after
let dest = std::env::temp_dir().join("out.jpg"); // reliable local path
if let Err(e) = save_image(&img, dest, 90) {
    eprintln!("falha ao salvar JPEG: {e:#}");
}
Defensive patterns

Strategy: try-catch

Validate before calling

let meta = std::fs::metadata(dest.parent().unwrap())?;
// ensure destination dir writable
let probe = dest.parent().unwrap().join(".write_probe");
std::fs::write(&probe, b"").map_err(|e| format!("destino sem permissão: {e}"))?
;let _ = std::fs::remove_file(&probe);

Try / catch

if let Err(e) = save_image(&img, dest, quality) {
    if e.to_string().contains("não gravei o JPEG") {
        eprintln!("falha ao escrever JPEG em {}: {e:#}", dest.display());
        // fallback to a temp dir or another volume
    }
}

Prevention

When it happens

Trigger: Calling `save_image` with a .jpg/.jpeg destination where JPEG encoding fails — typically an I/O error on the underlying writer (disk full, permissions revoked after create), or an image the JPEG encoder can't handle.

Common situations: Disk full when saving large result images; antivirus/backup tooling locking the freshly created file; destination on a flaky network mount.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of tonhowtf/omniget@8600b91f42 (2026-09-12). Data as JSON: /api/errors/a3b25a2f329b956a. Report an issue: GitHub.

Appendix: source

Thrown at src-tauri/omniget-core/src/core/tools/img_bg.rs:356

    if wants_jpeg && (has_background || mask_only) {
        "jpg"
    } else {
        "png"
    }
}

fn save_image(img: &DynamicImage, dest: &Path, ext: &str, quality: u8) -> anyhow::Result<u64> {
    if let Some(parent) = dest.parent() {
        std::fs::create_dir_all(parent).with_context(|| format!("criando {}", parent.display()))?;
    }
    if ext == "jpg" {
        let file =
            std::fs::File::create(dest).with_context(|| format!("criando {}", dest.display()))?;
        let mut w = std::io::BufWriter::new(file);
        let mut enc =
            image::codecs::jpeg::JpegEncoder::new_with_quality(&mut w, quality.clamp(1, 100));
        enc.encode_image(&img.to_rgb8())
            .map_err(|e| anyhow!("não gravei o JPEG {}: {}", dest.display(), e))?;
    } else {
        img.save_with_format(dest, image::ImageFormat::Png)
            .map_err(|e| anyhow!("não gravei o PNG {}: {}", dest.display(), e))?;
    }
    Ok(std::fs::metadata(dest).map(|m| m.len()).unwrap_or(0))
}

// ── Execução ───────────────────────────────────────────────────────────

/// Roda a inferência num arquivo já aberto e devolve a máscara no tamanho
/// original da imagem.
fn mask_for_image(
    session: &mut ort::session::Session,
    img: &DynamicImage,
    p: &BgParams,
) -> anyhow::Result<GrayImage> {
    let side = p.size as i64;
    // O `ort` traz um `ndarray` próprio (0.17) e o crate usa o 0.16, então o

View on GitHub (pinned to 8600b91f42)