Image Processing using Open Source Software

 Deepti Kuthari 
Intern at Indian Institute of Remote Sensing

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 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 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 fi rst program using PIL. Let us open an image of scenery in Python. For this you need to import image class and can follow the command Img = Image.open (scenery.jpg’). Make sure that your image and Python code are in the same folder. otherwise you need to specify the path of image file.

Fig. scenery.jpg
Code: sc_f.py
import Image ## to import Image
pic = Image.open('scenery.jpg')
print pic.format, pic.size, pic.mode 
pic.show()
pic.rotate(45).show()

" With the help of Python modules Numpy and Scipy, Python competes with other similar platforms for image processing. ”
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.

Code: sc_ro.py
import Image ## to import Image
pic = Image.open('scenery.jpg')
print pic.format, pic.size, pic.mode 
pic.rotate(45).show()

To convert and save a RGB image to greyscale, the following command can be used.

Code: sc_grey.py
import Image
pic = Image.open('scenery.jpg').convert('L')
pic.save('scenery_greyscale.jpeg','jpeg')


scenery_greyscale.jpg

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.

Code: sc_tmb.py 
import Image
pic= Image.open('scenery.jpg')
pic.thumbnail((128,128))
pic. save('sc_thumbnail.jpg','JPEG')

sc_thumbnail.jpg

To start with some image processing, let us make a ‘negative’ of the image ‘scenery’. Please try the following code. (For this you need to import two more libraries - ImageChops and ImageFilter).

Code: sc_neg.py
import Image
import ImageChops
import ImageFilter
pic = Image.open('scenery.jpg')
picNeg_scenery = ImageChops.invert(pic)
picNeg_scenery.save('Neg_scenery.jpg','JPEG')


Neg_scenery.jpg

Now let us see some more fi ltering techniques that can be done by using Python in-built classes. For the following filters, first 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 filters - BLUR filter, EMBOSS filter, CONTOUR fi lter, Find Edges Filter etc.

Code: sc_blur.py
import Image
import ImageChops
import ImageFilter
pic = Image.open('scenery.jpg')
ImBlur = pic.filter(ImageFilter.BLUR)
ImBlur.save('scenery_BLUR.jpg','JPEG')


scenery_BLUR.jpg

Code: sc_embos.py
import Image
import ImageChops
import ImageFilter
pic = Image.open('scenery.jpg')
ImEmb = pic.filter(ImageFilter.EMBOSS)
ImEmb.save('scenery_EMBOSS.jpg','JPEG')


scenery_EMBOSS.jpg

Code: sc_contour.py
import Image
import ImageChops
import ImageFilter
pic = Image.open('scenery.jpg')
ImContour = pic.filter(ImageFilter.CONTOUR)
ImContour.save('scenery_CONTOUR.jpg','JPEG')




scenery_CONTOUR.jpg

Code: sc_edge.py
import Image
import ImageChops
import ImageFilter
pic = Image.open('scenery.jpg')
ImEdges = pic.filter(ImageFilter.FIND_EDGES)
ImEdges = ImEdges.save('scenery_FIND_EDGES.jpg','JPEG')



scenery_FIND_EDGES.jpg

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.

Code: sc_arry.py
import Image
import numpy
import scipy
pic = Image.open('scenery.jpg')
array = numpy.asarray(pic)
print array

 Output :

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>================================RESTART=============================
>>> 
[[[ 55  17   0]
  [ 63  25   4]
  [ 72  34  13]
  ..., 
  [125 104  85]
  [142 122 111]
  [ 63  44  37]]

 [[ 56  18   0]
  [ 60  22   1]
  [ 63  25   4]
  ..., 
  [137 116  97]
  [137 117 106]
  [ 55  36  29]]

 [[ 64  26   5]
  [ 67  29   8]
  [ 65  27   6]
  ..., 
  [122  99  81]
  [117  97  86]
  [ 53  34  27]]

 ..., 
 [[167 139  91]
  [166 138  90]
  [164 136  88]
  ..., 
  [105  80  39]
  [102  85  57]
  [ 46  31   8]]

 [[168 140  92]
  [167 139  91]
  [165 137  89]
  ..., 
  [106  81  40]
  [103  86  58]
  [ 46  31   8]]

 [[168 140  92]
  [167 139  91]
  [166 138  90]
  ..., 
  [104  79  38]
  [101  84  56]
  [ 45  30   7]]]
>>> 


In this issue, we had a bird’s 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.
















Comments

Popular Posts