Add squezer for extracting exif data
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
use clap::Parser;
|
||||
use serde_json::{Map, Value};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn map_to_value(tag: exif::Tag, value: exif::Value) -> Value {
|
||||
if let Some(n) = value.get_uint(0) {
|
||||
return Value::Number(n.into());
|
||||
}
|
||||
if let exif::Value::Rational(rs) = value {
|
||||
return serde_json::json!({
|
||||
"num": Value::Number(rs[0].num.into()),
|
||||
"denom": Value::Number(rs[0].denom.into()),
|
||||
});
|
||||
}
|
||||
Value::String(value.display_as(tag).to_string())
|
||||
}
|
||||
|
||||
fn extract_exif_data(image_path: &PathBuf) -> Value {
|
||||
let image_file = std::fs::File::open(image_path).unwrap();
|
||||
let mut reader = std::io::BufReader::new(image_file);
|
||||
let exifreader = exif::Reader::new();
|
||||
let exif = exifreader.read_from_container(&mut reader).unwrap();
|
||||
|
||||
let mut exif_data = Map::new();
|
||||
for f in exif.fields() {
|
||||
let t = format!("{}", f.tag);
|
||||
exif_data.insert(t, map_to_value(f.tag, f.value.clone()));
|
||||
}
|
||||
Value::Object(exif_data)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Parser)]
|
||||
struct Cli {
|
||||
image_path: PathBuf,
|
||||
#[arg(long)]
|
||||
root: PathBuf,
|
||||
#[arg(long)]
|
||||
dist_path: PathBuf,
|
||||
#[arg(long)]
|
||||
key: String,
|
||||
#[arg(long)]
|
||||
thumbnail: PathBuf,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let exif_data = extract_exif_data(&cli.image_path);
|
||||
|
||||
let metadata = serde_json::json!({
|
||||
"exif": exif_data,
|
||||
"path": {
|
||||
"name": cli.image_path.file_name().unwrap().to_str(),
|
||||
"relativeToRoot": cli.image_path.strip_prefix(&cli.root).unwrap(),
|
||||
"distRelativeToRoot": cli.dist_path.strip_prefix(&cli.root).unwrap(),
|
||||
"thumbnailRelativeToRoot": cli.thumbnail.strip_prefix(&cli.root).unwrap(),
|
||||
},
|
||||
"key": cli.key,
|
||||
});
|
||||
println!("{}", metadata);
|
||||
}
|
||||
Reference in New Issue
Block a user