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

    Hi,

    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) {
    e.printStackTrace();
    }
    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) {
    e.printStackTrace();
    }

    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,

     

    Fred

    Frederic Marion-Poll

    Is this a solution?

    OMEXMLMetadata metaData = null;
    try {
    metaData = Loader.getOMEXMLMetaData(path);
    } catch (UnsupportedFormatException | IOException e) {
    e.printStackTrace();
    }
    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.

    Best,

    – Stephane

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

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