26

I am trying to find a way to get the number of channels of an image using Pillow. This seems rather trivial but I couldn't find it (the simple answer).

I know I can work it around with a minor overhead like (2 possibilities thought):

  • Convert to numpy and check array.shape
  • Check image.size[0]*image.size[1] against len(image.getdata())

so I am not really interested in finding a working solution but rather in accomplishing this using pillow.

The code I am using is straight forward:

from PIL import Image

image = Image.open(image_path)
image.size  # <- this gives the size of the image but not the channel as in numpy.

(609, 439)

I also found this approach inspired by this answer (which also imports overhead of course):

num_channel = len(image.split())

To me it seems really peculiar I cannot find this simple answer.

4
  • have you try image.mode? Image also have image.info & Image.getbands()
    – deadvoid
    Oct 24, 2018 at 7:24
  • So, maybe the simpler way would be to use image.mode I guess. image.info is not standardized and getbands() also seems a work around.
    – Eypros
    Oct 24, 2018 at 7:34
  • There is an attribute called layers (image.layers). But it seems that if you open a png image, 3 layers are assumed and this value is not set... Not sure if this is enough for you
    – m33n
    Oct 24, 2018 at 7:35
  • 2
    Most of the time I find myself just converting to the number of channels I want, so if I want RGB but the image may be a single-channel palette image, I just use im = Image.open('...').convert('RGB') What do you want the number of channels for, by the way? Oct 24, 2018 at 8:04

1 Answer 1

51

I decided to answer my own question (although I basically will sum up the comment of @cryptonome).

Well, when it comes to PIL the options as I get it are:

  • image.mode: returns a str containing the mode of the data read. Typical values are "RGB" and "L" for RGB and gray-scale images respectively. Modes are presented here.
  • im2.info: which returns a dict containing various information about the image. This is image format specific. For jpg images for example it (possibly) contains fields with keys: dpi, jfif, jfif_density, exif etc. More information about jpg images can be found here.
  • image.getbands(): which returns a tuple (even a 1 element one) containing all different channel present in the data. For a typical RGB image this would be ('R', 'G', 'B') and for a typical gray-scale image would be ('L',).

So, judging from the above the more concise method in my opinion would be to compare image.mode against L and RGB strings to find if an image is gray-scale or not or if the number of channels (as in this question) is the main question then a simple len(image.getbands()) would do the job.

Normally len(image.mode) will coincide with len(image.getbands()) and could be used in its place but since there is at least one mode YCbCr which contains 5 characters but only 3 channels (3x8-bit pixels, color video format) it's safer to use len(image.getbands()) I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.