Python PIL | getbands() and getextrema() method
Last Updated :
08 Mar, 2024
Python PIL library contains Image module in which variety of functions are defined. PIL.Image.Image.getbands()
This method is used to get the mode (bands) present in an image.
Syntax: PIL.Image.Image.getbands(image_object [valid image path])
Parameters:
It takes a parameter image_object i.e it the reference of the image which is opened using open() method or image path can also be mentioned.
Return value: Returns a tuple containing the name of each band in this image. For example, getbands on an RGB image returns (“R”, “G”, “B”).
Python3
from PIL import Image
im = Image. open (r "C:\Users\Admin\Pictures\images.png" )
im1 = Image.Image.getbands(im)
print ( "Multiband image" , im1)
im2 = Image. open (r "C:\Users\Admin\Pictures\singleband.png" )
im3 = Image.Image.getbands(im2)
print ( "Single band image" , im3)
|
Output:
Multiband image ('R', 'G', 'B')
Single band image ('P', )
PIL.Image.Image.getextrema() method –
Gets the minimum and maximum pixel values for each band in the image.
Syntax: PIL.Image.Image.getextrema(image_object [valid image path])
Parameters:
It takes a parameter image_object i.e it the reference of the image which is opened using open() method or image path can also be mentioned.
Return Value: For a single-band image, a 2-tuple containing the minimum and maximum pixel value. For a multi-band image, a tuple containing one 2-tuple for each band.
Python3
from PIL import Image
im = Image. open (r "C:\Users\Admin\Pictures\download.png" )
im1 = Image.Image.getextrema(im)
print ( "Multi band image " , im1)
im2 = Image. open (r "C:\Users\Admin\Pictures\singleband.png" )
im3 = Image.Image.getextrema(im2)
print ( "Single band image " , im3)
|
Output:
Multi band image ((73, 255), (0, 255), (0, 255))
Single band image (0, 123)
These images used in above article –
Multiband Images
Single band Image
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...