Python Imaging Library (pillow) is one of the popular libraries used for image processing. Python Imaging Library can be used to display image, create thumbnails, resize, rotation, convert between file formats, contrast enhancement, filter and apply other Digital image processing techniques etc. PIL supports image formats like PNG, JPEG, GIF, TIFF, BMP, etc. It also possesses powerful image processing and graphics capabilities. To start with image processing first we need to download PIL and install in PC. PIL supports python version 2.1 to 2.7.
One of the most important classes in Python Imaging Library is Image module. It contains an in-built function to perform the operations like- load images, save, change format of image and create new images. If your PC is ready with PIL, you can start your first program using PIL.
Let us open an image of water lily (download image from here) in Python. For this you need to import Image class and can follow the command
Img = Image.open(lily.jpg')
Make sure that your image and Python code are in the same folder. Otherwise you need to specify the path of image file.
import Image ## to import Image class Img = Image.open('lily.jpg') ## to open image-lily.jpg print Img.format, Img.size, Img.mode ## to print format, size mode Img.show() ## to show image in your image viewer
Now you can see image in your default image viewer. Here, the third line gives the format of image, size of image in pixels, and mode of the image (that is RGB or CYMK etc)
Now to rotate the image by an angle, the following command can be used
Img.rotate(45).show() ## to rotate image by 45 degree
To convert and save a RGB image to greyscale, the following command can be used.
import Image Img = Image.open('lily.jpg').convert('L') Img.save('lily_greyscale.jpeg',"jpeg")
We may come across some situation to resize images, or create a thumbnail of some image. Let’s see how this can be done using Python.
import Image Img = Image.open('lily.jpg') Img.thumbnail((128,128)) Img. save('lily_thumbnail.jpg’,"JPEG")
To start with some image processing, let us make a ‘negative’ of the image ‘lily’. Please try the following code. (For this you need to import two more libraries- ImageChops and ImageFilter)
import Image import ImageChops import ImageFilter Img = Image.open('lily.jpg') ImgNeg_lily = ImageChops.invert(Img) ImgNeg_lily.save('Neg_lily.jpg',"JPEG")
Now let us see some more filtering techniques that can be done by using Python in-built classes. For the following filters, first you need to import modules – Image, ImageChops, ImageFilter as in the previous example. After opening the image in python, by ‘Image. open’ method (line4 in previous example), we can use different filters – BLUR filter, EMBOSS filter, CONTOUR filter, Find Edges Filter etc.
***Use commands 1,2,3,4 in previous program here*** ImBlur = Img.filter(ImageFilter.BLUR) ImBlur.save('lily_BLUR.jpg',"JPEG") ***Use commands 1,2,3,4 in previous program here*** ImEmb = Img.filter(ImageFilter.EMBOSS) ImEmb.save('lily_EMBOSS.jpg',"JPEG") ***Use commands 1,2,3,4 in previous program here*** ImContour = Img.filter(ImageFilter.CONTOUR) ImContour.save('lily_CONTOUR.jpg',"JPEG") ***Use commands 1,2,3,4 in previous program here*** ImEdges = Img.filter(ImageFilter.FIND_EDGES) ImEdges = ImEdges.save('lily_FIND_EDGES.jpg',"JPEG")
You can convert an image into array for doing further operations which can be used for applying mathematical techniques like Fourier Transform; the following code can be used.
import Image import numpy import scipy pic = Image.open("lotus.jpg") array = numpy.asarray(pic) print array
There are many more exciting experiments that you can do with the image processing using Python. The power of Numpy and Scipy adds more advantages to image processing.