RustでExifのデータを読んで見る

概要

RustでExifのデータを読んで見る。Rustが全然かけないので、とりあえずがむしゃらにやって見る。です。作業メモ。

準備

Exifを読むためのライブラリを探したところ以下のものが比較的多くダウンロードされていそう。
https://github.com/felixc/rexiv2
このページに従って、コードを書いてみたがライブラリを読み込んでいないことを思い出した。

# console
$ cargo init

Cargo.tomlというものができるので、dependenciedにこのCrateを記載。

[package]
name = "exif"
version = "0.1.0"
authors = ["Katsumata Ryo <katsumata@stores.jp>"]
edition = "2018"

[dependencies]
rexiv2 = "0.7.0" // <- これ

buildしてみる

$ cargo run Updating crates.io index Downloaded rexiv2 v0.7.0
Downloaded gexiv2-sys v1.0.0
Downloaded num-rational v0.2.1
Downloaded libc v0.2.45
Downloaded pkg-config v0.3.14
Downloaded num-traits v0.2.6
Downloaded num-integer v0.1.39
Compiling num-traits v0.2.6
Compiling libc v0.2.45
Compiling num-integer v0.1.39
Compiling pkg-config v0.3.14
Compiling num-rational v0.2.1
Compiling gexiv2-sys v1.0.0
error: failed to run custom build command for gexiv2-sys v1.0.0
process didn't exit successfully: /Users/ryo/work/ryo/study_rust/exif/target/debug/build/gexiv2-sys-806d287d621f3889/build-script-build (exit code: 101) --- stdout
The gexiv2 library was not found by pkg-config on your system.

Consult the README.md file for suggestions on how to acquire it.

--- stderr
thread 'main' panicked at '`"pkg-config" "--libs" "--cflags" "gexiv2"` did not exit successfully: exit code: 1
--- stderr
Package gexiv2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gexiv2.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gexiv2' found
', /Users/ryo/.cargo/registry/src/github.com-1ecc6299db9ec823/gexiv2-sys-1.0.0/build.rs:28:13
note: Run with `RUST_BACKTRACE=1` for a backtrace.

warning: build failed, waiting for other jobs to finish...
error: build failed

どうやら gexiv2 がなさそう。これはRustのものじゃなくてそもそも別のライブラリっぽかったのでhomebrewでインストールできそうだった。

brew install gexiv2
==> Installing dependencies for gexiv2: exiv2, pcre and glib
==> Installing gexiv2 dependency: exiv2
==> Downloading <https://homebrew.bintray.com/bottles/exiv2-0.26.mojave.bottle.1>.
######################################################################## 100.0%
==> Pouring exiv2-0.26.mojave.bottle.1.tar.gz
🍺 /usr/local/Cellar/exiv2/0.26: 55 files, 7.6MB
==> Installing gexiv2 dependency: pcre
==> Downloading <https://homebrew.bintray.com/bottles/pcre-8.42.mojave.bottle.tar>
######################################################################## 100.0%
==> Pouring pcre-8.42.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/pcre/8.42: 204 files, 5.5MB
==> Installing gexiv2 dependency: glib
==> Downloading <https://homebrew.bintray.com/bottles/glib-2.58.2.mojave.bottle.t>
######################################################################## 100.0%
==> Pouring glib-2.58.2.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/glib/2.58.2: 435 files, 18.5MB
==> Installing gexiv2
==> Downloading <https://homebrew.bintray.com/bottles/gexiv2-0.10.9.mojave.bottle>
######################################################################## 100.0%
==> Pouring gexiv2-0.10.9.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/gexiv2/0.10.9: 22 files, 496KB

よっしゃ。

コード

とりあえずドキュメントを読んでみて、流儀に外れていてもとりあえず読み込めるものを作ることを頑張った。

use rexiv2;

fn main() {
let file = "/Users/ryo/Downloads/Photos/DSC04117.jpg";
let meta = rexiv2::Metadata::new_from_path(&file).unwrap();

println!("{:?}", meta.get_pixel_width());
println!("{:?}", meta.get_pixel_height());
println!("F: {:?}", meta.get_fnumber());
println!("ISO: {:?}", meta.get_iso_speed());
println!("{:?}", meta.get_exposure_time());
}

実行

$ cargo run
Compiling exif v0.1.0 (/Users/ryo/work/ryo/study_rust/exif)
Finished dev [unoptimized + debuginfo] target(s) in 0.72s
Running `target/debug/exif`
6000
4000
F: Some(1.8)
ISO: Some(320)
Some(Ratio { numer: 1, denom: 200 })

なんか変だけど、それっぽいのがとれている気がする!この記事では一旦ここまで。

今後

以下の事ができると自分がやりたいことにとりあえず追いつくので

  • 画像path直書きしたのを引数で取得する
  • someとかのところをstringで表示する
  • RatioがRatioで表示できてないのでstringで表示する
  • ボディ名とレンズ名を取得する

このあたりをやっていきます。