Python PIL | Image.split() 方法
PIL 是Python Imaging Library,它为Python解释器提供了图像编辑功能。
Image.split() 方法用于将图像分割成单独的波段。此方法从图像中返回单个图像波段的元组。
拆分“RGB”图像会创建三个新图像,每个图像都包含原始波段之一(红色、绿色、蓝色)的副本。
Syntax:
var = Image.Image.split(image_object)
OR
var = Image.Image.split(path_of_image)
Return Value: It returns a tuple containing bands.
代码#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()
输出:
使用的图像: