Extracting pixel values of an image in python

I wanted to extract each pixel values so that I can use them for locating simple objects in an image. Every image is made up of pixels and when these values are extracted using python, four values are obtained for each pixel (R, G, B, A). This is called the, G, B, A). This is called the RGBA color space having the Red, Green, Blue colors and Alpha value respectively. The Alpha values indicate the color space having the Red, Green, Blue colors and Alpha value respectively. The Alpha values indicate the

Check out my new article : Manipulating image pixels with python scikit image – color scales : how to convert from rgb, hsv , grayscale and also a tutorial on binarization.

The Alpha values indicate the transparency or the background factor in the image. a “.png” image, for example, can be without a background and is said to be transparent.

In python, we use a library called PIL (python imaging Library). The modules in this library are used for image processing and have support for many file formats like png, jpg, bmp, gif etc. It comes with a large number of functions that can be used to open, extract data, change properties, create new images and much more…

PIL comes pre-installed with python2.7 in ubuntu but for Windows, it has to be installed manually.But for either operating systems having python2.7 or more can be downloaded from here.and for python3 it can be downloaded from here.

each pixel value can be extracted and stored in a list.Though IDLE shell can be used for it, it can take a long time to extract the values and hence it’s recommended that it is done using command line interface.
The procedure for extraction is as follows:

1. import the Image module of PIL into the shell.

2. create an image object and open the image for the reading mode.Letmyfile’ is the name of the image to be read and give the appropriate file format. that is if it’s a jpeg image then give it as myfile.jpg.

3. we use a function of Image module called getdata() to extract the pixel values. this scans the image horizontally from left to right starting at the top-left corner.  The values got from each pixel is then added to a list. Finally, wat we get is a list with each pixel value as a set of 4 values (R, G, B, A).


Following is the code to achieve it:

from PIL import Image
im = Image.open('myfile.png', ' r')
pix_val = list(im.getdata())  #pix_val is the list that contains all
#the pixel values which can be printed to see those values

But the list got is a list of sets and sometimes it’s needed to flatten the list for example if the list is like:
[(123,124,145,120), (345,453,234,124),……] and the  list that is needed  is
[123, 124, 145, 120, 345, 453, 234, 124….] then the command to flatten the list is:

pix_val_flat = [x for sets in pix_val for x in sets]

This list comprehension extracts each element of each set in the list pix_val and all the elements are stored in pix_val_flat.

Thus this can be compiled into a script or made into a function which can be used in any image processing projects later. PIL is one of the many methods for image processing. pygames or numpy can be used with their respective modules to process images as well.


3 thoughts on “Extracting pixel values of an image in python

    1. There might be various reasons as to why that might have happened. First, it might be because the image file was very large causing some kind of an overflow leading to the GUI or shell to crash. Second, it might be because of the way you printed it… Try iterating through the list of pixels values and print values as you iterate through it. Feel free to leave a comment if you face any issues.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s