Using the image crate how do I retrieve the dpi of an image?

I want to convert px to pt (pdf points). Afaik I need to know the dpi of an image, if an image has been created and manipulated using the image: crates.io: Rust Package Registry how would I retrieve its dpi so I can scale it appropriately using the printpdf crate: printpdf - Rust

The dpi, or two pixel sizes, is often stored in EXIF data as XResolution/YResolution tags. You can access the raw data through the ImageDecoder trait that can be constructed with ImageReader::into_decoder. If you want to use the high-level functions for getting the image data itself, maybe you create another separate instance of ImageReader (many forms of possible io-inputs can be copied; we do demand Seek afterall). You'll want to parse the binary data, if it exists, with kamadak-exif afterwards as we don't bundle any particular crate here. Not all decoders provide access to (any) metadata so hopefully this applies to your images.

Disclaimer: from a maintainer side. Yes, we realize it's a bit of a pain. Next release is going to rework the trait and machinery on top to make metadata access a lot simpler in the normal decoding pipeline. No promises; but hopefully ~June.

2 Likes