Crate imghdr

Source
Expand description

Library that determines the type of image contained in a file or byte stream, basically the clone of the Python imghdr module.

§No-std support

Can be used in no-std environments with disabled std feature (enabled by default).

§Examples

Check the file directly:

match imghdr::from_file("./tests/images/example.png") {
    Ok(Some(imghdr::Type::Png)) => println!("Yep, it is a PNG"),
    Ok(..) => println!("Nope, it is definitely not a PNG"),
    Err(e) => println!("Some error happened: {:?}", e),
}

Or check the bytes stream:

let mut file = File::open("./tests/images/example.jpeg")?;
let mut content: Vec<u8> = vec![];
file.read_to_end(&mut content)?;

match imghdr::from_bytes(&content) {
    Some(imghdr::Type::Jpeg) => println!("And this is a Jpeg"),
    _ => println!("Can a Png, Bmp or other file format"),
}

It is not required to pass the fully read file into the crate functions, right now imghdr requires only first 12 bytes of contents for image format recognition.

Enums§

  • Recognized image types

Functions§

  • Try to determine image format from a bytes slice.
  • Open file and try to determine if it is an image.
  • Try to determine image format from an IO stream of bytes.