Read IPTC Alt Text From An Image In Rust With xmp_toolkit
March 2024
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.
```cargo
[dependencies]
anyhow ="1.0.86"
xmp_toolkit ="1.7.3"
```
useanyhow::Result;usestd::path::PathBuf;usexmp_toolkit::{xmp_ns, OpenFileOptions, XmpFile};fnmain(){let input =PathBuf::from("xmp-toolkit-al-text-test/photoshop-2024-iptc-alt-text.jpg");ifletOk(alt_text)=get_alt_text(input){println!("{}", alt_text);}}fnget_alt_text(path: PathBuf)->Result<String>{ifletOk(mut f)=XmpFile::new(){ifletOk(_)= f.open_file(
path.clone(),OpenFileOptions::default().only_xmp().use_smart_handler(),)
.or_else(|_err|{
f.open_file(path,OpenFileOptions::default().use_packet_scanning())}){ifletSome(xmp)= f.xmp(){ifletSome((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())}}
Output:
This is alt text embedded in an image from photoshop