/  Technology   /  Image Processing using SciPy and Python

## Image Processing using SciPy and Python

What is Image Processing?

Image processing is any form of processing for which the input is an image or a series of images or videos, such as photographs or frames of video. The output of image processing can be either an image or a set of characteristics or parameters related to the image.

SciPy

SciPy builds on the NumPy array object and is part of the NumPy stack which includes tools like Matplotlib, pandas and an expanding set of scientific computing libraries.

First install SciPy library using command

`pip install scipy`

Let’s see how we can read an image and display an image using SciPy and python.

1) Reading and Displaying an Image

Let’s see the syntax for reading an image in our IDE. You just need scipy and matplotlib to display the image

Syntax

```from scipy import misc
import matplotlib. pyplot as plt
plt. imshow (image)
plt. show ()```

Output

To read the image in a grayscale mode you just have to add one parameter while reading the image that is mode = “L” and then display the image

Output

2) Flip Image

Let’s see how we can flip the image upside down and left to right

Syntax

```from scipy import misc
import matplotlib. pyplot as plt
import numpy as np
image = misc. imread ("C:/Users/Desktop/cute-baby-animals-1558535060.jpg”, mode = "L")
flip_image_LR = np. fliplr (image)
flip_image_UP = np. flipud (image)
plt. imshow (image)
plt. show ()
plt. imshow(flip_image_LR)
plt. show ()
plt. imshow(flip_image_UP)
plt. show ()```

Output

Original Image

Left to right image

Upside down image

3) Blurring or Smoothing Effect

To give this effect we can use Gaussian filter.

Gaussian Filter

A Gaussian filter is a linear filter which is used to blur an image or to reduce its noise.

Let’s see an example

Syntax

```from scipy import misc, ndimage
import matplotlib. pyplot as plt
import numpy as np
blurred=ndimage. gaussian_filter (image, sigma=6)
plt.imshow(image)
plt.show()
plt. imshow (blurred)
plt. show ()```

Output

We can also use uniform filter for smoothing or blurring effect

Let’s see an example

Syntax

`ndimage. uniform_filter (image, size = 1, mode = “reflect”)`

There are different types of mode such as wrap, mirror, constant, nearest. By default, it is reflect.

Syntax

```from scipy import misc, ndimage
import matplotlib. pyplot as plt
import numpy as np
uniform = ndimage. uniform_filter (image, size = 10, mode = "reflect")
plt. imshow(image)
plt. show ()
plt. imshow(uniform)
plt. show ()```

Output

4) Geometrical Transformation

We can rotate, crop and flip the image using scipy

Syntax

```from scipy import misc, ndimage
import matplotlib.pyplot as plt
import numpy as np
rotate_noshape = ndimage. rotate (image, 45, reshape = False)
rotate = ndimage. rotate(image,45)
lx, ly, lz = image. shape
crop_face = image [lx // 3: - lx // 3, ly // 5: - ly // 5]
plt. imshow(image)
plt. show ()
plt. imshow(rotate)
plt. show ()
plt. imshow(rotate_noshape)
plt. show ()
plt. imshow (crop_face)
plt. show ()```

Output

In this way we have seen some basics of Image processing using scipy and python