Rust open image with other format than file extension

1.2k Views Asked by At

I am trying to open an image in my Rust program. The image is named foo.png, but it won't open the image with image::open("foo.png"). If I rename the file to foo.jpeg, I can open the image, so my guess is that the formatting and the file extension do not match.

My question is then. how do I open a file named foo.png, but decode it as a jpeg? I have tried image::io::Reader, but I can't seem to make it work properly.

1

There are 1 best solutions below

3
Brian Bowman On BEST ANSWER

You can use the image::load() function, which lets you specify the format:

let image = image::load(BufReader::new(File::open("foo.png")?), ImageFormat::Jpeg)?;