简体   繁体   English

pil(枕头)图像中的通道数

[英]Number of channels in pil (pillow) image

I am trying to find a way to get the number of channels of an image using Pillow.我正在尝试找到一种使用 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):我知道我可以用一些小的开销来解决这个问题(2 种可能的想法):

  • Convert to numpy and check array.shape转换为 numpy 并检查array.shape
  • Check image.size[0]*image.size[1] against len(image.getdata())检查image.size[0]*image.size[1]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) (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.对我来说,这似乎很奇怪,我找不到这个简单的答案。

I decided to answer my own question (although I basically will sum up the comment of @cryptonome).我决定回答我自己的问题(虽然我基本上会总结@cryptonome 的评论)。

Well, when it comes to PIL the options as I get it are:好吧,当涉及到 PIL 时,我得到的选项是:

  • image.mode : returns a str containing the mode of the data read. image.mode :返回包含读取数据模式的 str 。 Typical values are "RGB" and "L" for RGB and gray-scale images respectively. RGB 和灰度图像的典型值为"RGB""L" Modes are presented here . 此处介绍模式。
  • im2.info : which returns a dict containing various information about the image. im2.info :它返回一个包含有关图像的各种信息的字典。 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 .例如,对于 jpg 图像,它(可能)包含带有键的字段: dpijfifjfif_densityexif等。有关jpg图像的更多信息可以在这里找到。
  • image.getbands() : which returns a tuple (even a 1 element one) containing all different channel present in the data. image.getbands() :它返回一个元组(甚至是 1 个元素),其中包含数据中存在的所有不同通道。 For a typical RGB image this would be ('R', 'G', 'B') and for a typical gray-scale image would be ('L',) .对于典型的 RGB 图像,这将是('R', 'G', 'B')并且对于典型的灰度图像将是('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.因此,从以上判断,我认为更简洁的方法是将image.modeLRGB字符串进行比较,以查找图像是否为灰度或通道数(如本问题)是主要问题然后一个简单的len(image.getbands())就可以完成这项工作。

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.通常len(image.mode)将与len(image.getbands())重合,可以代替它使用,但因为至少有一种模式YCbCr包含 5 个字符但只有 3 个通道(3x8 位像素,彩色视频)格式)我猜使用len(image.getbands())更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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