Home

Read IPTC Alt Text From An Image In Rust With xmp_toolkit

rust
```cargo
[dependencies]
anyhow = "1.0.86"
xmp_toolkit = "1.7.3"
```

use anyhow::Result;
use std::path::PathBuf;
use xmp_toolkit::{xmp_ns, OpenFileOptions, XmpFile};

fn main() {
  let input = PathBuf::from("xmp-toolkit-al-text-test/photoshop-2024-iptc-alt-text.jpg");
  if let Ok(alt_text) = get_alt_text(input) {
    println!("{}", alt_text);
  }
}

fn get_alt_text(path: PathBuf) -> Result<String> {
  if let Ok(mut f) = XmpFile::new() {
    if let Ok(_) = f.open_file(
      path.clone(),
      OpenFileOptions::default().only_xmp().use_smart_handler(),
    )
    .or_else(|_err| {
        f.open_file(path, OpenFileOptions::default().use_packet_scanning())
    }) {
      if let Some(xmp) = f.xmp() {
        if let Some((value, _actual_lang)) =
          xmp.localized_text(xmp_ns::IPTC_CORE, "AltTextAccessibility", Some("en"), "en-US")
        {
          Ok(value.value)
        } else {
          Ok("TODO: Figure out how to make this an error".to_string())
        }
      } else {
        Ok("TODO: make this an error".to_string())
      }
    } else {
      Ok("TODO: make this an error".to_string())
    } 
  } else {
      Ok("TODO: make this an error".to_string())
  }
}
results start

The other field is:

rust
ExtDescrAccessibility

This is my first run at reading IPTC Alt Text and Extended Description metadata embedded in an image from Photoshop. I don't have a good grasp on error handling in rust yet. So, the code could use some help, but it gets the data.

~ fin ~

References