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 With ImageMagick In Rust

This is the process I'm using to get the width and height of images for a project I'm working on. It uses ImageMagick instead of the `image`` crate. I'm using it because it's considerably faster.

Code

use std::process::Command;

fn main() {
    let path = "/Users/alan/workshop/__sources/tmp/pixel_frame.png";
    let height = image_height(path);
    dbg!(height);
}

fn image_height(img: &str) -> u32 {
    let args = ["-format", "%h", img];
    let response = Command::new("identify").args(args).output().unwrap();
    let height_string = String::from_utf8(response.stdout).unwrap();
    height_string.parse::<u32>().unwrap()
}

That call is for the height. Changing `%h`` to `%w`` provides with width.

Speedy image dimensions in Rust with ImageMagick