shibukawa / imagesize_py

MIT License
222 stars 43 forks source link

Getting the number of channels? #58

Open Erotemic opened 1 year ago

Erotemic commented 1 year ago

Is it possible to add functionality to also parse out the number of channels in the image? I'd like to distinguish between grayscale, RGB, and RGBA images. It would be OK it it got confused by things like color pallets.

The reason is that I'd like to incorporate this into my kwimage.load_image_shape function as it is stupidly faster than PIL and GDAL:

        >>> # For large files, PIL is much faster GDAL
        >>> from osgeo import gdal
        >>> from PIL import Image
        >>> import timerit
        >>> #
        >>> import kwimage
        >>> fpath = kwimage.grab_test_image_fpath()
        >>> #
        >>> ti = timerit.Timerit(100, bestof=10, verbose=2)
        >>> for timer in ti.reset('gdal'):
        >>>     with timer:
        >>>         gdal_dset = gdal.Open(fpath, gdal.GA_ReadOnly)
        >>>         width = gdal_dset.RasterXSize
        >>>         height = gdal_dset.RasterYSize
        >>>         gdal_dset = None
        >>> #
        >>> for timer in ti.reset('PIL'):
        >>>     with timer:
        >>>         pil_img = Image.open(fpath)
        >>>         width, height = pil_img.size
        >>>         pil_img.close()
        >>> # The imagesize module is quite fast
        >>> import imagesize
        >>> for timer in ti.reset('imagesize'):
        >>>     with timer:
        >>>         width, height = imagesize.get(fpath)
Timed gdal for: 100 loops, best of 10
    time per loop: best=83.266 µs, mean=85.919 ± 2.1 µs
Timed PIL for: 100 loops, best of 10
    time per loop: best=38.191 µs, mean=38.981 ± 0.7 µs
Timed imagesize for: 100 loops, best of 10
    time per loop: best=8.269 µs, mean=8.516 ± 0.2 µs

But in those use-cases it's often important to know how many channels there will be as well. Is that possible to parse out of the headers?