How to get size of image without reading it in memory?

Home Forums Development How to get size of image without reading it in memory?

  • Frederic Marion-Poll


    I would like to aggregate images of different sizes into a sequence (typically 20 images). To do so, I would like to find the image with the largest width and height, and then, adjust the size of all the other images, using an algorithm like this: (here, to adjust them to the size of a rectangle called “rectRef”):

    IcyBufferedImage ibufImage = null;
    try {
    ibufImage = Loader.loadImage(filename);  // load image in memory
    } catch (UnsupportedFormatException | IOException e) {
    ibufImage = IcyBufferedImageUtil.getSubImage(ibufImage, rectRef ); // change size of the image
    try {
    Saver.saveImage(ibufImage, files.get(i), true); // save the file again on the disk
    } catch (FormatException | IOException e) {

    However, this is quite lengthy, especially if the images are TIFF and large, because all files need to be loaded in memory at least once.

    Is it possible to read only the header of TIFF files and extract the height and width?


    Thank you in advance for any help/suggestion,



    Frederic Marion-Poll

    Is this a solution?

    OMEXMLMetadata metaData = null;
    try {
    metaData = Loader.getOMEXMLMetaData(path);
    } catch (UnsupportedFormatException | IOException e) {
    int imageWidth = MetaDataUtil.getSizeX(metaData, 0);
    int imageHeight= MetaDataUtil.getSizeY(metaData, 0);

    Stephane Dallongeville

    Hi Frederic,

    Yeah, that is the method to use: load metadata and read it. Depending image file format even metadata can take a bit of time to read but it will be much faster than loading all image data itself.


    – Stephane

Viewing 3 posts - 1 through 3 (of 3 total)

The forum ‘Development’ is closed to new topics and replies.