📜  Python PIL | Image.split() 方法

📅  最后修改于: 2022-05-13 01:54:54.757000             🧑  作者: Mango

Python PIL | Image.split() 方法

PIL 是Python Imaging Library,它为Python解释器提供了图像编辑功能。

Image.split() 方法用于将图像分割成单独的波段。此方法从图像中返回单个图像波段的元组。
拆分“RGB”图像会创建三个新图像,每个图像都包含原始波段之一(红色、绿色、蓝色)的副本。

代码#1:

# importing Image class from PIL package
from PIL import Image
  
# opening a multiband image (RGB specifically)
im = Image.open(r"C:\Users\Admin\Pictures\network.png")
  
# split() method
# this will split the image in individual bands
# and return a tuple
im1 = Image.Image.split(im)
  
# showing each band
im1[0].show()
im1[1].show()
im1[2].show()

输出:

代码#2:

# importing Image class from PIL package
from PIL import Image
  
# opening a singleband image
im = Image.open(r"C:\Users\Admin\Pictures\singleband.png")
  
# split() method
# this will split the image in individual bands
# and return a tuple (of 1 element for singleband)
im1 = Image.Image.split(im)
  
# showing image
im1[0].show()

输出:

使用的图像: