0

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?

RoganJosh
  • 5
  • 3
  • Are they always a specific format, e.g. TIFF, GIF, PNG, BMP? If so, there may be something much lighter weight than the whole ImageMagick suite? What platform(s) are you on? – Mark Setchell Dec 17 '20 at 21:37

1 Answers1

0

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, ...)

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36