Image Processing Using Python How To
-
Upload
arul-selvam -
Category
Documents
-
view
114 -
download
0
description
Transcript of Image Processing Using Python How To
CSI Communications | December 2012 | 23CSI Communications | December 2012 | 23
Let’s 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 fi le formats, contrast
enhancement, fi 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 fi rst 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 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 fi le.
1 import Image ## to import Image class
2 Img = Image.open(‘lily.jpg’) ## to open image-lily.jpg
3 print Img.format, ## to print format,Img.size, Img.mode size mode
4 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.1 Img.rotate(45).show() ## to rotate image
by 45 degreeTo convert and save a RGB image to greyscale, the following
command can be used.
1 import Image2 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. Let’s see how this can be done using
Python.
1 import Image2 Img = Image.open(‘lily.jpg’)3 Img.thumbnail((128,128))4 Img. save(‘lily_thumbnail.jpg’,”JPEG”)
lily_greyscale.jpg
To start with some image processing, let us make a ‘negative’ of
the image ‘lily’.
Practitioner Workbench
Umesh PDepartment of Computational Biology and Bioinformatics, University of Kerala
Programming.Learn (“Python”) »
Image Processing in Python
With the help of Python modules Numpy and Scipy, Python competes with other similar platforms for image processing.
Neg_lily.jpg
lily.jpg
CSI Communications | December 2012 | 24 www.csi-india.orgCSI Communications | December 2012 | 24 www.csi-india.orgg
Please try the following code. (For this you need to import two
more libraries - ImageChops and ImageFilter)
1 import Image2 import ImageChops3 import ImageFilter4 Img = Image.open(‘lily.jpg’)5 ImgNeg_lily = ImageChops.invert(Img)6 ImgNeg_lily.save(‘Neg_lily.jpg’,”JPEG”)
Now let us see some more fi ltering techniques that can be done
by using Python in-built classes. For the following fi lters, fi 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 diff erent fi lters - BLUR fi lter, EMBOSS fi lter,
CONTOUR fi lter, Find Edges Filter etc.
***Use commands 1,2,3,4 in previous program here*** ImBlur = Img.fi lter(ImageFilter.BLUR)ImBlur.save(‘lily_BLUR.jpg’,”JPEG”)
***Use commands 1,2,3,4 in previous program here*** ImEmb = Img.fi lter(ImageFilter.EMBOSS)ImEmb.save(‘lily_EMBOSS.jpg’,”JPEG”)
***Use commands 1,2,3,4 in previous program here***
ImContour = Img.fi lter(ImageFilter.CONTOUR)ImContour.save(‘lily_CONTOUR.jpg’,”JPEG”)
***Use commands 1,2,3,4 in previous program here*** ImEdges = Img.fi lter(ImageFilter.FIND_EDGES)ImEdges = ImEdges.save(‘lily_FIND_EDGES.jpg’,”JPEG”)
‘lily.jpg’ after applying the CONTOUR fi lter
‘lily.jpg’ after applying the FIND EDGES fi 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.
1 import Image2 import numpy3 import scipy4 pic = Image.open(“lotus.jpg”)5 array = numpy.asarray(pic)6 print array
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.
n
lily.jpg’ after applying the BLUR fi lter
‘lily.jpg’ after applying the EMBOSS fi lter