Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the number of channels? #58

Open
Erotemic opened this issue Jan 4, 2023 · 0 comments
Open

Getting the number of channels? #58

Erotemic opened this issue Jan 4, 2023 · 0 comments

Comments

@Erotemic
Copy link

Erotemic commented Jan 4, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant