ANimator120
ANimator120

Reputation: 3401

Get File Size of Image in Rust

How can you determine the file size of an image using the image crate in rust?

let img = image::open("imgs/2.jpg").unwrap();
let myBytes = &img.to_bytes();
//get the number of bytes?

Upvotes: 0

Views: 2452

Answers (2)

gil.fernandes
gil.fernandes

Reputation: 14601

As of March 2023 you can consider to use also the std::path::PathBuf class.

Here is an example on how you can read the file size in bytes:

Welcome to evcxr. For help, type :help
>> use std::path::PathBuf;
>> let path = PathBuf::from("root/images/mountain-landscape.jpg");
>> let len = path.metadata().unwrap().len();
>> println!("File length: {len}");
File length: 421631

or in one line:

>> std::path::PathBuf::from("root/images/mountain-landscape.jpg").metadata().unwrap().len()
421631

Upvotes: 1

Alexey S. Larionov
Alexey S. Larionov

Reputation: 7927

Let image crate deal with images, let standard library's fs module deal with the file system:

let imgSize = std::fs::metadata("imgs/2.jpg").unwrap().len();

Also look for other capabilities of std::fs::Metadata struct

Upvotes: 6

Related Questions