#image-format #image #png #jpeg #webp #byte-stream #gif

no-std imghdr

Library that determines the type of image contained in a file or byte stream

11 releases (6 breaking)

0.7.0 May 8, 2019
0.6.0 May 8, 2019
0.5.0 May 6, 2019
0.4.0 Jul 20, 2016
0.1.4 Jul 18, 2016

#356 in Images

Download history 309/week @ 2023-12-10 532/week @ 2023-12-17 276/week @ 2023-12-24 12/week @ 2023-12-31 775/week @ 2024-01-07 316/week @ 2024-01-14 362/week @ 2024-01-21 447/week @ 2024-01-28 671/week @ 2024-02-04 1057/week @ 2024-02-11 917/week @ 2024-02-18 736/week @ 2024-02-25 544/week @ 2024-03-03 824/week @ 2024-03-10 665/week @ 2024-03-17 988/week @ 2024-03-24

3,029 downloads per month
Used in 2 crates

Apache-2.0 OR MIT

21KB
119 lines

imghdr

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

Latest Version Latest Version Rustc Version 1.31+ Travis Build Status Apache 2.0 OR MIT licensed

Examples

Check the file directly:

# extern crate imghdr;
# fn main() {
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:

# extern crate imghdr;
# use std::fs::File;
# use std::io::{self, Read};
#
# fn main() -> io::Result<()> {
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"),
}

# Ok(())
# }

No runtime deps