Python:Pillow .getpixel()
The .getpixel() method is part of the Image module in the Pillow library.
It takes (x, y) coordinates as a tuple and returns the pixel value at that location. If the image is grayscale (mode "L"), it returns an integer. For an RGB image, it returns a tuple (R, G, B), and for an RGBA image, it returns (R, G, B, A).
Syntax
Image.getpixel(xy)
xy(tuple or list of two integers): The(x, y)coordinates of the pixel to retrieve.
Example
Here is the image to be used:
![]()
The following example demonstrates the usage of the .getpixel() method:
# Import the Image module from the PIL libraryfrom PIL import Image# Open the image file and store it in the img variableimg = Image.open('peacock.png')# Get the RGBA value of the pixel at position (50, 50)rgba_value = img.getpixel((50, 50))# Print the RGBA valueprint(rgba_value)
The above code produces the following output:
(69, 89, 46, 255)
The output (69, 89, 46, 255) represents the RGBA values of the pixel at (50, 50) in the image. The values indicate that the red component is 69, the green component is 89, the blue component is 46, and the alpha component is 255, meaning the pixel is fully opaque. Since the image is in RGBA mode, .getpixel() returns four values instead of three.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pillow on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours