Experiments with the Python Imaging Library

Back in February 2009, I spent some time playing with the Python Imaging Library (PIL). I even started a blog post on my experiments with PIL, but I never finished it. I will finish it now. Be warned: this post will have a very experimental flavor.

Since we are all acquainted with Lena, as a test image I will use the following photograph of the late Ruslana Korshunova (1987-2008):

The flowers and the hair create plenty of high-frequency content. Let us now use the Python Imaging Library (PIL) to convert the image to B&W, invert the image, and apply several elementary filters to it.

__________

Python script

The following Python script opens the image above and performs the aforementioned operations on it:

import Image
import ImageChops
import ImageFilter

# open image and print format
Im = Image.open('Ruslana.jpg')
print Im.format, Im.size, Im.mode

# convert to black & white
ImBW = Im.convert('1')
ImBW.save('Ruslana_BW.bmp',"BMP")

# invert image (obtain negative)
ImInv = ImageChops.invert(Im)
ImInv.save('Ruslana_Inv.jpg',"JPEG")

# apply BLUR filter
ImBlur = Im.filter(ImageFilter.BLUR)
ImBlur.save('Ruslana_BLUR.jpg',"JPEG")

# apply CONTOUR filter
ImContour = Im.filter(ImageFilter.CONTOUR)
ImContour.save('Ruslana_CONTOUR.jpg',"JPEG")

# apply DETAIL filter
ImDetail = Im.filter(ImageFilter.DETAIL)
ImDetail.save('Ruslana_DETAIL.jpg',"JPEG")

# apply EDGE_ENHANCE filter
ImEH = Im.filter(ImageFilter.EDGE_ENHANCE)
ImEH.save('Ruslana_EDGE_ENHANCE.jpg',"JPEG")

# apply EDGE_ENHANCE_MORE filter
ImEHM = Im.filter(ImageFilter.EDGE_ENHANCE_MORE)
ImEHM.save('Ruslana_EHM.jpg',"JPEG")

# apply EMBOSS filter
ImEmb = Im.filter(ImageFilter.EMBOSS)
ImEmb.save('Ruslana_EMBOSS.jpg',"JPEG")

# apply FIND_EDGES filter
ImEdges = Im.filter(ImageFilter.FIND_EDGES)
ImEdges = ImEdges.save('Ruslana_FIND_EDGES.jpg',"JPEG")

# apply SMOOTH filter
ImSmooth = Im.filter(ImageFilter.SMOOTH)
ImSmooth = ImSmooth.save('Ruslana_SMOOTH.jpg',"JPEG")

# apply SMOOTH_MORE filter
ImSmoothMore = Im.filter(ImageFilter.SMOOTH_MORE)
ImSmoothMore = ImSmoothMore.save('Ruslana_SMOOTH_MORE.jpg',"JPEG")

# apply SHARPEN filter
ImSharp = Im.filter(ImageFilter.SHARPEN)
ImSharp = ImSharp.save('Ruslana_SHARPEN.jpg',"JPEG")

Let us now see how the processed images look like.

__________

Results

Here’s the B&W image:

The script produced a monochromatic (1 bit per pixel) BMP file. Since WordPress.com does not allow one to upload BMP files, I converted it to JPEG, which greatly increased the size of the file. How ironic! In this case, lossy compression actually led to expansion!

The inverted (i.e., negative) image looks quite interesting:

It looks as though the EDGE_ENHANCE_MORE filter performs some form of amplification of the high-frequency content of the image:

Look how funny the flowers and her hair look! If we want to extract the actual edges, we can use the FIND_EDGES filter:

The other images produced by the Python script are not particularly interesting and, therefore, I have not posted them here.

__________

Concluding remarks

The Python Imaging Library (PIL) offers the capability to perform some very basic image processing. However, given Python’s lack of a matrix data structure, one cannot do that much. I believe the PIL is useful to process large collections of photos (to generate thumbnails, for example), not to perform 2-dimensional signal processing.

__________

Related:

Tags: , ,

8 Responses to “Experiments with the Python Imaging Library”

  1. nic Says:

    Well, Python by itself doesn’t have a native matrix class, but it doesn’t open images either! You should use numpy and scipy if you want to do anything more complicated.

  2. Conor Says:

    I agree with nic. All you need is the following:

    import numpy
    Arr=numpy.array(Im,numpy.uint8)

  3. Alejandro Weinstein Says:

    1. You should be using a picture of Fabio :)

    http://nuit-blanche.blogspot.com/2012/03/i-cant-believe-its-not-lena.html

    http://nuit-blanche.blogspot.com/2012/03/let-there-be-only-one-fabio.html

    2. You might be interested in scikits-image or SimpleCV.

    • Rod Carvalho Says:

      I find the photo of Miss Korshunova more aesthetically pleasing than the one of Fabio ;-) Thanks for the links, by the way. I did not know of SimpleCV.

  4. pebbie Says:

    or OpenCV ?

  5. Shubhendu Trivedi Says:

    You should remove this image, or most probably you’d get labeled a sexist pig.

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 )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 76 other followers

%d bloggers like this: