It's probably best to use the Python Image Library to do this which I'm afraid is a separate download.

The easiest way to do what you want is via the load() method on the Image object which returns a pixel access object which you can manipulate like an array:

from PIL import Image

im = Image.open('dead_parrot.jpg') # Can be many different formats.
pix = im.load()
print im.size  # Get the width and hight of the image for iterating over
print pix[x,y]  # Get the RGBA Value of the a pixel of an image
pix[x,y] = value  # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png')  # Save the modified pixels as .png

Alternatively, look at ImageDraw which gives a much richer API for creating images.

Answer from David Webb on Stack Overflow
Top answer
1 of 13
255

It's probably best to use the Python Image Library to do this which I'm afraid is a separate download.

The easiest way to do what you want is via the load() method on the Image object which returns a pixel access object which you can manipulate like an array:

from PIL import Image

im = Image.open('dead_parrot.jpg') # Can be many different formats.
pix = im.load()
print im.size  # Get the width and hight of the image for iterating over
print pix[x,y]  # Get the RGBA Value of the a pixel of an image
pix[x,y] = value  # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png')  # Save the modified pixels as .png

Alternatively, look at ImageDraw which gives a much richer API for creating images.

2 of 13
62

Using Pillow (which works with Python 3.X as well as Python 2.7+), you can do the following:

from PIL import Image
im = Image.open('image.jpg', 'r')
width, height = im.size
pixel_values = list(im.getdata())

Now you have all pixel values. If it is RGB or another mode can be read by im.mode. Then you can get pixel (x, y) by:

pixel_values[width*y+x]

Alternatively, you can use Numpy and reshape the array:

>>> pixel_values = numpy.array(pixel_values).reshape((width, height, 3))
>>> x, y = 0, 1
>>> pixel_values[x][y]
[ 18  18  12]

A complete, simple to use solution is

# Third party modules
import numpy
from PIL import Image


def get_image(image_path):
    """Get a numpy array of an image so that one can access values[x][y]."""
    image = Image.open(image_path, "r")
    width, height = image.size
    pixel_values = list(image.getdata())
    if image.mode == "RGB":
        channels = 3
    elif image.mode == "L":
        channels = 1
    else:
        print("Unknown mode: %s" % image.mode)
        return None
    pixel_values = numpy.array(pixel_values).reshape((width, height, channels))
    return pixel_values


image = get_image("gradient.png")

print(image[0])
print(image.shape)

Smoke testing the code

You might be uncertain about the order of width / height / channel. For this reason I've created this gradient:

The image has a width of 100px and a height of 26px. It has a color gradient going from #ffaa00 (yellow) to #ffffff (white). The output is:

[[255 172   5]
 [255 172   5]
 [255 172   5]
 [255 171   5]
 [255 172   5]
 [255 172   5]
 [255 171   5]
 [255 171   5]
 [255 171   5]
 [255 172   5]
 [255 172   5]
 [255 171   5]
 [255 171   5]
 [255 172   5]
 [255 172   5]
 [255 172   5]
 [255 171   5]
 [255 172   5]
 [255 172   5]
 [255 171   5]
 [255 171   5]
 [255 172   4]
 [255 172   5]
 [255 171   5]
 [255 171   5]
 [255 172   5]]
(100, 26, 3)

Things to note:

  • The shape is (width, height, channels)
  • The image[0], hence the first row, has 26 triples of the same color
🌐
Medium
medium.com › embedded-connected-avinya-intelligence › unlocking-color-codes-a-beginners-guide-to-extracting-rgb-values-from-images-with-python-dd6f8010dba2
“Unlocking Color Codes: A Beginner’s Guide to Extracting RGB Values from Images with Python” | by Aniket Fasate | Embedded & Connected | Medium
June 8, 2025 - rgb_values = [] # Use a list comprehension to populate the list with RGB values of each pixel. rgb_values = [img.getpixel((x, y)) for y in range(height) for x in range(width)] # Print the RGB value of the first pixel. print("RGB value of the ...
🌐
Jon Witts' Blog
jonwitts.co.uk › archives › 839
Extract RGB Values from an Image | Jon Witts' Blog
May 16, 2016 - # Import the PIL library - pip3 install Pillow from PIL import Image import turtle # Open our image im = Image.open("landscape.jpg") # Convert our image to RGB rgb_im = im.convert('RGB') # Use the .size object to retrieve a tuple contain ...
🌐
YouTube
youtube.com › watch
Extracting RGB values from Image in Python | Colorgram Package | Python for Beginners #lec117 - YouTube
🔥 Master Generative AI from Scratch – GenAI Course for Beginners 🚀 | Start Your AI Career in 2025 🔗 GenAI Course for Beginners Link : https://www.jennysle...
Published: November 7, 2023
🌐
GitHub
github.com › libvips › libvips › discussions › 2715
How do I read the RGB values of an image? · libvips/libvips · Discussion #2715
I'm trying to follow this guide on how to make an ASCII Art program, I'm currently stuck at finding out how I would get the RGB values for every pixel in an image. Am I right to assume that it would have to do something about bands? Since I've read that bands usually represent color. How would I be able to access this? Thanks! ... There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... import pyvips x = pyvips.Image.new_from_file("something.jpg") # jpegs can be eg.
🌐
YouTube
youtube.com › er karan arora
How to Extract the RGB Values of a Pixel in OpenCV using Python - YouTube
In this video, I'll show you How to Extract the RGB Values of a Pixel in OpenCV using Python#OpenCV #OpenCVPython OpenCVTutorials #Python #ComputerVision►Itr...
Published: July 13, 2021
Views: 7K
🌐
Stack Overflow
stackoverflow.com › questions › 46801824 › get-rgb-data-from-image-files-using-python › 46802237
get rgb data from image files using python - Stack Overflow
I can do very simple things. enter image description here when i run this code, I can get just one pixel data. I want to get all of the rgb values from image file. and save it as txt file How can...
Find elsewhere
🌐
DEV Community
dev.to › chukwunazaekpere › finding-out-the-rgb-value-of-an-image-with-python-12af
Finding out the RGB value of an image with python - DEV Community
October 25, 2021 - ... from PIL import Image import os filename = os.path.join(os.path.dirname(__file__), "your_image_filename.format") # ensure that the image exists in your current working path with Image.open(filename) as rgb_image: image_color = rgb_image.getp...
🌐
Medium
shegocodes.medium.com › extracting-all-colors-in-images-with-python-2e36eb8a67d2
Extracting All Colors in Images with Python | by Shegocodes | Medium
October 21, 2024 - Parameters ---------- image_path : str path to imagefile for detection Return ------ sorted list of tuples (color, total number detections) """ # Read image image = Image.open(image_path) # Convert image into RGB image = image.convert('RGB') # Get ...
🌐
Codementor
codementor.io › community › basic image processing in python - part 1
Basic Image Processing In Python - Part 1 | Codementor
September 15, 2019 - In image processing tools, for ... and increasing processing time as there’s less information in the images. There are a couple of ways to do this in python to convert image to grayscale. But a straight forward way using matplotlib is to take the weighted mean of the RGB value of original ...
🌐
YouTube
youtube.com › pygpt
how to extract rgb values from an image python - YouTube
Download this code from https://codegive.com Certainly! Extracting RGB values from an image in Python can be done using various libraries like PIL (Python Im...
Published: December 25, 2023
Views: 4
🌐
Machinelearning
machinelearning.org.in › home › computer vision
Extracting The Image RGB Values of a Pixel in Python Using OpenCV – Machine Learning Tutorials, Courses and Certifications
April 4, 2021 - print('Type of the image : ' , type(img)) print() print('Shape of the image : {}'.format(img.shape)) print('Image Hight {}'.format(img.shape[0])) print('Image Width {}'.format(img.shape[1])) print('Dimension of Image {}'.format(img.ndim))
🌐
Towards Data Science
towardsdatascience.com › home › data science › image color extraction with python in 4 steps
Image Color Extraction with Python in 4 Steps | Towards Data Science
May 25, 2022 - Next, let's import extcolors and rgb2hex libraries. The extcolors library returns the RGB values, which will be converted into HEX color codes with the rgb2hex library.
🌐
Curbal
curbal.com › curbal-learning-portal › extract-colors-from-a-web-image-using-python
Extract colors from a web image using Python – Curbal
April 14, 2023 - In this video I am going to show you how you can extract the colors from an element in your data visualization, so you can use it elsewhere in your visual. 00:00 Intro 00:37 Import the colors and the images 01:40 Convert the colors to RGB pixels and the the frequency 02:20 Store the RGB colors in a dataframe and pick the ones I want 05:15 Convert the RGB to HEX 05:50 Plot the colors with Matplotlib 06:00 Add a complementary color to list of colors
🌐
YouTube
youtube.com › idan python
How to get RGB of a pixel in python - YouTube
the code:from PIL import Imagedef rgb_of_pixel(img_path, x, y): im = Image.open(img_path).convert('RGB') r, g, b = im.getpixel((x, y)) a = (r, g, b)...
Published: November 7, 2020
Views: 18K
🌐
Alteryx Community
community.alteryx.com › t5 › Alteryx-Designer-Desktop-Discussions › Read-RGB-colors-from-every-pixel-of-an-image › td-p › 896085
Solved: Read RGB colors from every pixel of an image - Alteryx Community
January 28, 2022 - from ayx import Alteryx import pandas as pd from PIL import Image def rgb_of_pixel(img_path, x, y): im = Image.open(img_path).convert('RGB') r,g,b = im.getpixel((x,y)) a = (r,g, b) return a img = r"C:\Users\Desktop\download.png" table = [ [rgb_of_pixel(img, 5, 5)] ] dataframe = pd.DataFrame(table) Alteryx.write(dataframe, 1)
🌐
Nuevofoundation
workshops.nuevofoundation.org › python-pixel › colors_and_pixels › access-pixel
Access pixels on image
Color[0] gives the red value of the pixel, Color[1] gives the green value of the pixel, and Color[2] gives the blue value of the pixel. # Remember to import Image from PIL import Image img=Image.new('RGB', (200,100),"yellow") color=img.getpixel((100,50)) # Let's print out the RGB value of a ...