TechnikTobi / little_exif

A little library for reading and writing EXIF data in pure Rust.
Apache License 2.0
22 stars 5 forks source link

Cannot recover the String value of a specific metadata #5

Closed Alexsilva43 closed 1 month ago

Alexsilva43 commented 9 months ago

Hello, I try to recover the String value of a specific metadata of an image, but I cannot find any method for this in the doc.

I've found this in git examples:

for tag in Metadata::new_from_path(jpg_path).unwrap().data()
    {
        println!("{:?}", tag);
    }

However, it's a loop over all the tags to "force" the printing of an ExifTag type.

If I want to get the String value in the ImageDescription metadata (for instance), how to proceed?

I've tried the method get_tag but it returns an ExifTag, not the String metadata.

TechnikTobi commented 9 months ago

Hi,

yes, you are correct, the get_tag function returns an ExifTag struct at first, which needs to be further processed to get the data you want (as this could be e.g. the ISO, Image Dimensions, Image Description, etc.). Trying to come up with some example code I actually stumbled upon a problem ("how tf do I get the endianness?") that got fixed a few minutes ago with v0.3.1. You can now get the ImageDescription string as follows (I also added this to the examples section):

use little_exif::metadata::Metadata;
use little_exif::endian::U8conversion;

let metadata = Metadata::new_from_path(path).unwrap();
let image_description_tag = metadata.get_tag(&ExifTag::ImageDescription(String::new())).unwrap();

// Alternatively you can get the tag via its hex value:
// let image_description_tag = metadata.get_tag_by_hex(0x010e).unwrap();

let image_description_string = String::from_u8_vec(
    &image_description_tag.value_as_u8_vec(metadata.get_endian()),
    metadata.get_endian()
);

println!("{}", image_description_string);

I'll admit that this is a bit cumbersome - if you have a suggestion on how to make this more straightforward let me know!

Best, Tobi

Alexsilva43 commented 9 months ago

Thanks for the reply. I have a question about how to use the image to include the metadata. In your examples, you've used the path of the image. Actually, I am in Webassembly context and it doesn't work very well when we try to access the system environment. So, I'm using the byte representation of an image (&[u8]). Is there a way to add metadata to the image from this format?

TechnikTobi commented 1 month ago

A bit late, but: If you have the image as a Vec<u8> you can do something like this:

let mut image_data = fs::read(&input_path)?;
let file_extension = FileExtension::PNG;
let mut metadata = Metadata::new_from_vec(&image_data, file_extension)?;

metadata.set_tag(ExifTag::ImageDescription("test".to_string()));
metadata.write_to_vec(&mut image_data, file_extension)?;

fs::write(output_path, file_content)?;