The Wayback Machine - https://web.archive.org/web/20240203030031/https://www.geeksforgeeks.org/python-pil-getpixel-method/
Open In App
Related Articles

Python PIL | getpixel() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The PixelAccess class provides read and write access to PIL.Image data at a pixel level. 
Accessing individual pixels is fairly slow. If you are looping over all of the pixels in an image, there is likely a faster way using other parts of the Pillow API. 
getpixel() Returns the pixel at x, y. The pixel is returned as a single
 

Syntax: getpixel(self, xy)
Parameters:
xy :The pixel coordinate, given as (x, y).
Returns: a pixel value for single band images, a tuple of pixel values for multiband images.
 

Image Used: 
 

 

Python3




# Importing Image from PIL package
from PIL import Image
 
# creating a image object
im = Image.open(r"C:\Users\System-Pc\Desktop\leave.jpg")
px = im.load()
print (px[4, 4])
px[4, 4] = (0, 0, 0)
print (px[4, 4])
coordinate = x, y = 150, 59
 
# using getpixel method
print (im.getpixel(coordinate));


Output: 
 

(130, 105, 49)
(0, 0, 0)
(75, 19, 0)

Another example:Here we change the coordinate value. 
Image Used 
 

 

Python3




# Importing Image from PIL package
from PIL import Image
 
# creating a image object
im = Image.open(r"C:\Users\System-Pc\Desktop\leave.jpg")
px = im.load()
print (px[4, 4])
px[4, 4] = (0, 0, 0)
print (px[4, 4])
coordinate = x, y = 180, 79
 
# using getpixel method
print (im.getpixel(coordinate));


Output: 
 

(130, 105, 49)
(0, 0, 0)
(22, 168, 25)

 


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 19 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials