Home > Blockchain >  Why do I get a `DuplicateChunk` decoding error when using the image crate?
Why do I get a `DuplicateChunk` decoding error when using the image crate?

Time:02-28

I'm using image to open and read images in Rust. I used the Windows Snipping tool to save a screenshot as an image, and tried opening that

let image_path = "C:/Users/mason/rust-projects/image-test/image.png";
let image = image::open(image_path).unwrap();
let _ = image;

But I get the error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Decoding(DecodingError { format: Exact(Png), underlying: Some(Format(FormatError { inner: DuplicateChunk { kind: ChunkType { type: gAMA, critical: false, private: false, reserved: false, safecopy: false } } })) })', src\main.rs:82:12
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/30b3f35c420694a4f24e5a4df00f06073f4f3a37\/library\std\src\panicking.rs:584
   1: core::panicking::panic_fmt
             at /rustc/30b3f35c420694a4f24e5a4df00f06073f4f3a37\/library\core\src\panicking.rs:143
   2: core::result::unwrap_failed
             at /rustc/30b3f35c420694a4f24e5a4df00f06073f4f3a37\/library\core\src\result.rs:1749
   3: enum$<core::result::Result<enum$<image::dynimage::DynamicImage>,enum$<image::error::ImageError> > >::unwrap<enum$<image::dynimage::DynamicImage>,enum$<image::error::ImageError> >
             at /rustc/30b3f35c420694a4f24e5a4df00f06073f4f3a37\library\core\src\result.rs:1065
   4: image_test::main
             at .\src\main.rs:3
   5: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/30b3f35c420694a4f24e5a4df00f06073f4f3a37\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\image-test.exe` (exit code: 101)

Process finished with exit code 101

If I try opening a photo I took with my phone, it works fine. How can I open the screenshot in rust?

CodePudding user response:

I was able to reproduce. This appears to be due to a bug introduced in the png crate version 0.17.4. You don't have to manually manipulate the image and should instead simply use the previous version:

[dependencies]
image = "0.24.0"
png = "=0.17.3"
  • Related