PIL in Python


What is PIL?

I have been lately interested on image processing for personal purposes. My first thought was to go directly with Matlab… but, besides it needs like 5gb+ to download, it’s licenced… so it wasn’t an option anymore.

After a little research of tools for image processing i found an easy one… Python and his library “Python Imaging Library”, better known as just “PIL”. PIL basically adds image processing capabilities to our Python interpreter. This library supports many file formats, and provides powerful image processing and graphics capabilities.

We can get PIL directly from the official download page –> here, or directly from our console using the pip program, just like this: $pip install pillow

My specific objective to look for an image processing tool was just to remove background from several images. More specific… obtain the image’s silhouette, which resulted very easy to implement in python.

Obtaining Silhouettes!

The first step in this process is to import our PIL library in our python file (.py).

from PIL import Image

After that, we are ready to open up our source image specifying the directory, name and extension.

picture = Image.open("/dir/to/img/name.png-jpg")

To use PIL, we need to work with a pixels matrix, to do this just do the next.

pix = picture.load()

If we want to save a new image, we could just get a copy of our original image file, and its pixel matrix to handle it, like this:

new_pic = Image.new("RGB", picture.size)
new_pix = np.load()


and then we are ready to iterate the matrix. But… we still do not have image’s heigh and weigh. We can get them as easy as follow.

width, height = picture.size

and for iteration, as we are used to, just need two nested loops.

for x in range(0,width):
   for y in range(0,height):

We must be careful when getting r,g,b values (pixel colors). If we deal with jpg images, we can just extract three values: R,G and B. If we deal with png images, we need to extract an extra value, known as alpha, which determines the transparency level of a pixel. So, inside our loops, we do something like this:

r,g,b = pix[x,y]  //for jpg images
r,g,b,a = pix[x,y]  //for png images

And then we can start filtering the image. For instance, when dealing when png images its kind of easy to get the silhouette. Every pixel which is not totally transparent its part of the image’s background, so our filter looks like this:

/+With this condition we are just setting black all pixels contained in the interest area (not background). And every background pixel (transparent) are turned white.*/

if a != 0:
    new_pix[x,y] = (0,0,0)
    new_pix[x,y] = (255,255,255)

For jpg images, we concentrate just in those where the background is white. This allow us to set a threshold which let pass all pixels from gray to black. And turns the nearly white pixels to a different color, let’s say red.

if (r < 200 and g < 200 and b < 200: //if is not nearly white 
    new_pix[x,y] = (255,0,0)
    new_pix[x,y] = (255,255,255)

Finally, we just save the image specifying the path and name.


And that’s it. We can get the next fancy results after compiling our program:

Captura de pantalla 2015-12-09 a las 12.57.18

Captura de pantalla 2015-12-09 a las 13.00.20


Leave a Reply

Your email address will not be published. Required fields are marked *