tonhowtf/omniget · error
não gravei o PNG
Error message
não gravei o PNG: {} What it means
Thrown in write_image when encoding a sliced/packed sprite cell (or batch output) to PNG in memory fails. The `image` crate's write_to returned an error, which is wrapped with context naming the failed PNG write.
Solutions
- Check the underlying image::ImageError message in the context string for the exact encoding problem.
- Convert the image to a supported color type before writing (e.g. to_rgba8, which this path already applies via ImageRgba8).
- Ensure output directories are writable and there is disk space before calling write_image.
- If JPEG was intended, confirm the format argument maps to jpg so the encode_jpeg branch is taken.
Example fix
// before
DynamicImage::ImageRgba8(img.clone())
.write_to(&mut buf, image::ImageFormat::Png)
.map_err(|e| anyhow!("não gravei o PNG: {}", e))?;
// after
let img = DynamicImage::ImageRgba8(img.clone());
img.write_to(&mut buf, image::ImageFormat::Png)
.map_err(|e| anyhow!("não gravei o PNG: {}", e))?; // adds explicit rgba8 + logging of the error chain Defensive patterns
Strategy: try-catch
Try / catch
match write_image(&img, &path, quality) {
Err(e) if format!("{e}").contains("não gravei o PNG") => {
tracing::error!("falha ao codificar PNG {}: {e:#}", path.display());
// fallback: escrever como PNG via codec alternativo ou reportar ao usuário
}
other => other?,
} Prevention
- Always normalize images to Rgba8 before encoding PNG.
- Check free disk space and directory writability before batch writes.
- Keep image-crate features enabled for the formats you write.
- Log the full error chain to distinguish encode vs fs errors.
When it happens
Trigger: Calling write_image (via slice, pack, or batch) with a format that is not jpg, where DynamicImage::write_to(.., ImageFormat::Png) fails — typically an unsupported color type/encoding parameters combination, or an out-of-memory allocation for huge buffers.
Common situations: Writing very large spritesheets exceeding memory limits; unusual image color formats passed through; disk-full scenarios manifesting as encode/IO errors in downstream fs::write.
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/46be3e8eb1fdce82.
Report an issue: GitHub.
Appendix: source
Thrown at src-tauri/omniget-core/src/core/tools/image_sprite.rs:300
}
}
fn ext_of(format: &str) -> &'static str {
if format.eq_ignore_ascii_case("jpeg") || format.eq_ignore_ascii_case("jpg") {
"jpg"
} else {
"png"
}
}
fn write_image(img: &RgbaImage, path: &Path, format: &str, quality: u8) -> anyhow::Result<u64> {
let data = if ext_of(format) == "jpg" {
super::image_compress::encode_jpeg(&DynamicImage::ImageRgba8(img.clone()), quality)?
} else {
let mut buf = std::io::Cursor::new(Vec::new());
DynamicImage::ImageRgba8(img.clone())
.write_to(&mut buf, image::ImageFormat::Png)
.map_err(|e| anyhow!("não gravei o PNG: {}", e))?;
buf.into_inner()
};
if let Some(parent) = path.parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
std::fs::write(path, &data)?;
Ok(data.len() as u64)
}
/// JSON de atlas no formato de mapa (o mesmo shape que o TexturePacker usa no
/// preset "JSON (Hash)"), que é o que engine e bundler já sabem ler.
pub fn atlas_json(frames: &[SpriteFrame], sheet: &str, w: u32, h: u32) -> String {
let mut map = serde_json::Map::new();
for f in frames {
map.insert(
f.name.clone(),View on GitHub (pinned to 8600b91f42)