Sei sulla pagina 1di 2

Practitioner Workbench

Umesh P
Department of Computational Biology and Bioinformatics, University of Kerala

Programming.Learn (Python)

Image Processing in Python


Lets have a look into one more capability of Python programming - image processing. Being a powerful programming language with easy syntax, and extensible to C++ or Java, it is suitable for developing embedded applications. Image processing is extremely important in Python Platform. With the help of Python modules Numpy and Scipy, Python competes with other similar platforms for image processing. Python Imaging Library (PIL) is one of the popular libraries used for image processing. PIL can be used to display image, create thumbnails, resize, rotation, convert between le formats, contrast enhancement, lter 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 rst we need to download PIL and install in PC. PIL supports python version 2.1 to 2.7. 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.
1 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.
1 import Image 2 Img = Image.open(lily.jpg).convert(L) 3 Img.save(lily_greyscale.jpeg,jpeg)

We may come across some situation to resize images, or create a thumbnail of some image. Lets see how this can be done using Python.
1 2 3 4

import Image Img = Image.open(lily.jpg) Img.thumbnail((128,128)) Img. save(lily_thumbnail.jpg,JPEG)

lily.jpg One of the most important classes in PIL is image module. It contains an in-built function to perform operations like - load images, save, change format of image, and create new images. If your PC is ready with PIL, you can start your rst program using PIL. Let us open an image of water lily 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 le.
1 import Image ## to import Image class 2 Img = Image.open(lily.jpg) ## to open imagelily.jpg 3 print Img.format, ## to print format, Img.size, Img.mode size mode 4 Img.show() ## to show image in your image viewer

lily_greyscale.jpg To start with some image processing, let us make a negative of the image lily.

With the help of Python modules Numpy and Scipy, Python competes with other similar platforms for image processing.
Neg_lily.jpg

CSI Communications | December 2012 | 23

ImContour = Img.lter(ImageFilter.CONTOUR) ImContour.save(lily_CONTOUR.jpg,JPEG) ***Use commands 1,2,3,4 in previous program here*** ImEdges = Img.lter(ImageFilter.FIND_EDGES) ImEdges = ImEdges.save(lily_FIND_EDGES. jpg,JPEG)

lily.jpg after applying the BLUR lter Please try the following code. (For this you need to import two more libraries - ImageChops and ImageFilter)
1 2 3 4 5 6

import Image import ImageChops import ImageFilter Img = Image.open(lily.jpg) ImgNeg_lily = ImageChops.invert(Img) ImgNeg_lily.save(Neg_lily.jpg,JPEG)

lily.jpg after applying the CONTOUR lter

Now let us see some more ltering techniques that can be done by using Python in-built classes. For the following lters, rst you need to import modules - Image, ImageChops, and ImageFilter as in the previous example. After opening the image in python, by Image. open method (line 4 in previous example), we can use different lters - BLUR lter, EMBOSS lter, CONTOUR lter, Find Edges Filter etc.

lily.jpg after applying the FIND EDGES lter 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. lily.jpg after applying the EMBOSS lter
***Use commands 1,2,3,4 in previous program here*** ImBlur = Img.lter(ImageFilter.BLUR) ImBlur.save(lily_BLUR.jpg,JPEG) ***Use commands 1,2,3,4 in previous program here*** ImEmb = Img.lter(ImageFilter.EMBOSS) ImEmb.save(lily_EMBOSS.jpg,JPEG) ***Use commands 1,2,3,4 in previous program here***
1 2 3 4 5 6

import Image import numpy import scipy pic = Image.open(lotus.jpg) array = numpy.asarray(pic) print array

In this issue, we had a birds eye view of digital image processing using Python. 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. n

CSI Communications | December 2012 | 24

www.csi-india.org g

Potrebbero piacerti anche