Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Get The Width And Height Of An Image In Rust

I'm using the image crate^1^^ to get the width and height of images for a project. It's a few lines of code that looks like this:

Code

use image::io::Reader;

fn main() {
    let img = Reader::open("example.png")
        .unwrap()
        .decode()
        .unwrap();
    let width = img.width();
    let height = img.height();
    dbg!(width);
    dbg!(height);
}

The process is pretty slow. It can take a second or two for the values to return. Doing it with ImageMagick^2^^ is much faster. The raw ImageMagick command for the command line looks like this:

Code

identify -format "%w" pixel_frame.png

That can be used in a Rust `Command`rust` call for a faster turn around if ImageMagick is installed.

Footnotes

  • The Rust