Pre-processing images is most important part while making programs related to image or optical recognition. People are loving and migrating to python from other technologies. One of them is myself. Because I found python very much suitable for image recognition application on which I’m working. So in this article I want to describe about Image pre-processing with python. You may have to install some modules before starting like numpy and PIL.

Why pre-processing:

When we use learning networks to recognize images, The image must be in same format for which we have trained the network. So pre-processing is very much important to make image more precise and accurate. Steps in pre-processing may be resizing, cropping, changing hue, making black and white etc.

Image that I have at start is like this.

Letter B pre-processing

Now I want to show few pre-processing steps as per my need. Firstly I want to show how we can convert this into grey scale image.

Convert image into grey-scale.

Converting image into grey scale is very much easiest in python. You can directly convert into grey scale while reading. In grey scale Image, each pixel is represented by eight bit. Lowest value is 0 which means black color. Also Highest possible value for pixel is 255, Which represents white color.

We can see the contents by converting it to array. Have a look at following code and output too.

This statement read image from given path and convert image into grey scale. L inside convert method means grey scale.

This statement save the same image in given path specified. Now it will look something like this.


Scale Image.

Scaling Image is very much easy using PIL api. First import the scaleimage from Image module and then scale your image in desired way.

There are different resize method among them One is cover. Others are ‘contain’, ‘thumbnail’, ‘height’, ‘crop’, ‘width’.

Convert Image to black and white:

You can convert image into binary just like grey scale in first topic. But In this image my not look perfect black and white. Because most part that seems like white may contain small black spots. Then image seems noisy. So I prefer to make Image black and white with each pixel 8 bit.

lets have a look how you can convert image into binary image and how it look like.

Here 1 represents binary image. For the same image as in above example binary image after conversion looks like this.



if you want to look at the pixel values of this image it my look like this.

In the binary image every pixel is represented by a boolean value so Output looks like;-


[ True True True True False True True False True True False True
True False True True True True True True True False True True
False False True False True False True False True False True False
tFalse True False True False False True False True True True True
False True True True False True True True True True True True
True True True True True True True True True False True True
True True True]

For this I have to choose a threshold so that color value above that will be white and below that is black. For example if I chose 200 as threshold all the pixel having value greater than 200 will be white. And All the pixels having value less then 200 will be converted black. For making pixel black simply replace corresponding value by 0 and for making pixel white replace value with 255.

Now code for this is like this.

remember here the statement which copy array is important because the array from image are immutable. If you missed that statement you will encounter value error saying ‘ ValueError: assignment destination is read-only’.

fromarray method convert array back into image and put L ast mode which means image type is grey scale.

Now Image will look like this.

Image pre-processing with python.

Nice to see you here reading my article. These are my simple tricks for pre-processing but You may have more suitable tricks and will be glad if you share with me via email or by commenting down. Happy learning.

Facebook Comments