Expand description
Library to read IPTC tags from JPEG files, in pure Rust.
§Example
use iptc::IPTC;
use iptc::IPTCTag;
use std::error::Error;
use std::path::Path;
fn main() -> Result<(), Box<dyn Error>> {
let image_path = Path::new("tests/smiley.jpg");
// Reading IPTC metadata from file
let mut iptc = IPTC::read_from_path(&image_path)?;
// Alternatively, you can read from a buffer
// let buffer = std::fs::read(&image_path)?;
// let mut iptc = IPTC::read_from_buffer(&buffer)?;
// See all the tags in the image
println!("IPTC: {:?}", iptc.get_all());
// Get a specific tag
let keywords = iptc.get(IPTCTag::Keywords);
println!("keywords: {}", keywords);
// Writing new metadata
// For repeatable fields like Keywords, you can add multiple values
let keywords = vec!["rust", "metadata", "iptc"];
for keyword in keywords {
iptc.set_tag(IPTCTag::Keywords, keyword);
}
// For single-value fields, just set them directly
iptc.set_tag(IPTCTag::City, "Oslo");
iptc.write_to_file(&image_path)?;
// Alternatively, you can write to a buffer
// let buffer = std::fs::read(&image_path)?;
// let updated_buffer = iptc.write_to_buffer(&buffer)?;
// std::fs::write("output.jpg", updated_buffer)?;
Ok(())
}