Image Processing Using Python How To
Image Processing Using Python How To
Image Processing Using Python How To
Umesh P
Department of Computational Biology and Bioinformatics, University of Kerala
Programming.Learn (Python)
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
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
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)
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
www.csi-india.org g