Extracting pixel values of an image using python pil library

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. In this tutorial, I have tried to extract the pixels from a given image using python pil library.

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.

We use a library called python 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… You must note that there are much more advanced libraries now for image processing in python and that perform complex tasks like color scale translations, template matching, histogram related operations, noise removal etc. One such example of an advanced python library is the Scikit Image.

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. Python PIL library is one of the many methods for image processing. pygames or numpy can be used with their respective modules to process images as well.

If you found this article useful, then do share it on facebook, Google plus, twitter, etc. Comment below if you want to know more of this.

 

Advertisements

akshay pai

I am a data science engineer and I love working on machine learning problems. I have experience in computer vision, OCR and NLP. I love writing and sharing my knowledge with others. This is why I created Source Dexter. Here I write about Python, Machine Learning, and Raspberry Pi the most. I also write about technology in general, books and topics related to science. I am also a freelance writer with over 3 years of writing high-quality, SEO optimized content for the web. I have written for startups, websites, and universities all across the globe. Get in Touch! We can discuss more.

3 thoughts on “Extracting pixel values of an image using python pil library

  • August 28, 2013 at 3:38 am
    Permalink

    Reblogged this on Bite Sized Python Tips and commented:
    This is a very nice post which teaches you some of the basics of PIL (python imaging library) . Make sure that you read it if you are interested in images.

    Reply
  • September 20, 2015 at 1:01 pm
    Permalink

    Python GUI stopped executing after I added a
    print pix_val_flat
    or print pix_val

    Reply
    • September 20, 2015 at 11:50 pm
      Permalink

      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.

      Reply

Leave a Reply

Advertisements
%d bloggers like this: