简体   繁体   中英

Read in image as grayscale, but need the number of colour channels in the original colour image

I am using OpenCV Python to read in images. I read them in as grayscale as reading in the full colour image is expensive. However, I still need to identify the number of colour channels in the original image.

Is there a simple method of extracting the number of colour channels in an image using EXIF tags, PIL or any other libraries without reading in the full colour image?

imagemagick: $ magick identify -ping <file> reads as little as possible of a file and dumps a few basic properties of the picture file. discussion: https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=18042

you can run and read that using python's subprocess module. check_output() is probably most useful here.

I've just tried that on a 5 GB TIFF file and it runs in no time at all.

you will have to interpret that line somewhat. it says here "8-bit sRGB", which would imply three channels. sRGB is an RGB color space.

you can pass -format... and a format string to get custom output.

imagemagick has a command line interface but I hear it also has APIs you can call from python. other answer with details on that: Can I access ImageMagick API with Python?

if that's unsuitable you will need to use specific libraries (libjpeg, libpng, libtiff, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM