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.
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.
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.
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.
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.