Python PIL Image.getdata()

Python PIL Image.getdata()

PIL is the Python Imaging Library, which provides image editing capabilities for the Python interpreter. The image module provides a class of the same name to represent a PIL image. The module also provides factory functions for loading images from files and creating new images.

getdata() Returns the contents of this image as a sequence object containing pixel values. The sequence object is tiled, so the values in row 1 directly follow those in row 0, and so on.

Note that the sequence object returned by this method is an internal PIL data type that supports only certain sequence operations. To convert it to a normal sequence (for example, for printing), use list(im.getdata()).

Syntax: Image.getdata(band=None)

Parameters:

band – Which band to return. Defaults to returning all bands. To return a single band, pass in the index (e.g., 0 to get the “R” band from an “RGB” image).

Return Type : A sequence-like object.

Images used:
Python PIL Image.getdata()

# importing Image module from PIL package
from PIL import Image
   
# opening a image
im = Image.open(r"C:UsersSystem-PcDesktoplion.png").convert("L")
   
# getting colors
# multiband images (RBG)
im1 = Image.Image.getdata(im)
   
print(im1)

Output:

ImagingCore object at 0x0000026E11CD52D0

Another example: Here we modify the image.
Images used:
Python PIL Image.getdata()

# importing Image module from PIL package
from PIL import Image
   
# opening a image
im = Image.open(r"C:UsersSystem-PcDesktoptree.jpg").convert("L")
   
# getting colors
# multiband images (RBG)
im1 = Image.Image.getdata(im)
   
print(im1)

Output:

ImagingCore object at 0x0000029BA596C230

Leave a Reply

Your email address will not be published. Required fields are marked *