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:





July 14, 2012 at 18:50 |
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.
July 14, 2012 at 22:55 |
I agree with nic. All you need is the following:
import numpy
Arr=numpy.array(Im,numpy.uint8)
July 15, 2012 at 07:18 |
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.
July 15, 2012 at 07:36 |
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.
August 6, 2012 at 18:47 |
or OpenCV ?
August 13, 2012 at 14:33 |
You should remove this image, or most probably you’d get labeled a sexist pig.
August 14, 2012 at 03:57 |
There are over 7 billion people on this planet. I cannot make everyone happy. I do not even try.
August 14, 2012 at 04:18 |
LOL! I was just “trolling”, rather being playful.
Somebody wrote to me that if I find the Lenna story amusing (which it is) then I am a sexist pig, so was just warning you about the possibility of all out war. ;-)