Image Processing with SciPy and NumPy in Python

Image Processing with SciPy and NumPy

Prerequisite for Image Processing with SciPy and NumPy

For image processing with SciPy and NumPy, you will need the libraries for this tutorial. We checked in the command prompt whether we already have these:

Let’s Revise Range Function in Python — Range() in Python

  1. C:\Users\lifei>pip show scipy

Name: scipy

Version: 1.0.0

Summary: SciPy: Scientific Library for Python


Author: SciPy Developers


License: BSD

Location: c:\users\lifei\appdata\local\programs\python\python36–32\lib\site-packages

Requires: numpy


  1. C:\Users\lifei>pip show numpyName: numpy

Version: 1.14.1

Summary: NumPy: array processing for numbers, strings, records, and objects.


Author: NumPy Developers


License: BSD

Location: c:\users\lifei\appdata\local\programs\python\python36–32\lib\site-packages


Required-by: scipy, pandas, matplotlib

If you don’t already have them installed, use the following commands:

  1. pip install scipy
  2. pip install numpy

Also, some methods like imsave() did not show up with scipy in our interpreter. So, we used pip to install Pillow. Works for us.

  1. C:\Users\lifei>pip install Pillow

Collecting Pillow

Downloading (1.4MB)

100% |████████████████████████████████| 1.4MB 499kB/s

Installing collected packages: Pillow

Successfully installed Pillow-5.1.0

  1. C:\Users\lifei>pip install matplotlib

Collecting matplotlib

Downloading (8.5MB)

100% |████████████████████████████████| 8.5MB 448kB/s

Requirement already satisfied: numpy>=1.7.1 in c:\users\lifei\appdata\local\programs\python\python36–32\lib\site-packages (from matplotlib) (1.14.1)

Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib)

Downloading (56kB)

100% |████████████████████████████████| 61kB 222kB/s

Collecting cycler>=0.10 (from matplotlib)


Requirement already satisfied: pytz in c:\users\lifei\appdata\local\programs\python\python36–32\lib\site-packages (from matplotlib) (2018.3)

Collecting kiwisolver>=1.0.1 (from matplotlib)

Downloading (44kB)

100% |████████████████████████████████| 51kB 610kB/s

Requirement already satisfied: six>=1.10 in c:\users\lifei\appdata\local\programs\python\python36–32\lib\site-packages (from matplotlib) (1.11.0)

Requirement already satisfied: python-dateutil>=2.1 in c:\users\lifei\appdata\local\programs\python\python36–32\lib\site-packages (from matplotlib) (2.7.2)

Requirement already satisfied: setuptools in c:\users\lifei\appdata\local\programs\python\python36–32\lib\site-packages (from kiwisolver>=1.0.1->matplotlib) (39.2.0)

Installing collected packages: pyparsing, cycler, kiwisolver, matplotlib

Successfully installed cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.2 pyparsing-2.2.0

Let’s get to the Desktop:

  1. >>> import os
  2. >>> os.chdir(‘C:\\Users\\lifei\\Desktop’)

Read About Methods in Python — Classes, Objects and Functions in Python

Reading and Writing to Images

Before we start image processing with SciPy and NumPy, we must be able to open it. This is how we opn it:

  1. >>> from scipy import misc
  2. >>> f=misc.face()
  3. >>> misc.imsave(‘demo.png’,f)

imsave needs you to have the library PIL installed in your system. It lets you save an array as an image. This creates an image on our Desktop.

Now, we import pyplot from matplotlib.

  1. >>> import matplotlib.pyplot as plt
  2. >>> plt.imshow(f)
  3. <matplotlib.image.AxesImage object at 0x0864E050>
  4. >>>

This makes the following image of a raccoon face show up:

Image Processing with SciPy and NumPy — Reading and Writing to Images

Read About Python Sets and Booleans with Syntax and Examples

a. Creating a Python numpy array

Continuing where we left:

  1. >>> f=misc.imread(‘demo.png’)
  2. >>> type(f)
  3. <class ‘numpy.ndarray’>
  4. >>> f.shape #This gives us the size of the array. The image is 1024×768.
  5. (768, 1024, 3)
  6. >>> f.dtype
  7. dtype(‘uint8’) #This means it is an 8ibit image (0–255)

b. Opening a RAW file

A RAW file is one with the extension .raw. It is a camera image file holding minimally processed data from an image sensor. The sensor can be of a digital camera, a motion picture film scanner, or an image scanner.

Let’s first create such a file.

  1. >>> f.tofile(‘demo.raw’)

This creates the following icon on the Desktop:

Now, we import another module- np.

  1. >>> import np
  2. >>> fromraw=np.fromfile(‘demo.raw’,dtype=np.uint8)
  3. >>> fromraw.shape
  4. (2359296,)
  5. >>> fromraw.shape=(768, 1024, 3)

For much larger data, we can use memory mapping:

  1. >>> memmap=np.memmap(‘demo.raw’,dtype=np.uint8,shape=(768,1024,3))

This reads data from the file but does not load it into memory.

Let’s Read About Python Date and Time — Syntax and examples

c. Working on more than one image at once

  1. >>> for i in range(7):
  2. im=np.random.randint(0,256,10000).reshape((100,100))
  3. misc.imsave(‘random_%02d.png’%i,im)
  4. >>> from glob import glob
  5. >>> filelist=glob(‘random*.png’)
  6. >>> filelist.sort()

Get to your Desktop to find the following image files:

SciPy and NumPy

Let’s Learn Python Strings with String Functions and String Operations

Displaying Images

  1. >>> f1=misc.face(gray=True) #For a grayscale image
  2. >>> plt.imshow(f1,
  3. <matplotlib.image.AxesImage object at 0x0919E810>

We can use min and max values to increase contrast:

  1. >>> plt.imshow(f1,,vmin=30,vmax=200)
  2. <matplotlib.image.AxesImage object at 0x0919E9B0>
  3. >>> plt.axis(‘off’) #This removes axes and ticks
  4. (-0.5, 1023.5, 767.5, -0.5)

a. Drawing Contour Lines

We call the contour() method to draw contour lines on the image.

  1. >>> plt.contour(f1,[50,200])
  2. <matplotlib.contour.QuadContourSet object at 0x019954F0>

Image Processing with SciPy and NumPy — Drawing Contour Lines

b. Interpolation

Adjusting the ‘interpolation’ argument, we can adjust the intensity variation. By setting it to ‘bilinear’, we get smooth intensity variations, and by setting it to ‘nearest’, we get a fine inspection of intensity variations.

Do You Know About Python Decision Making Statements with Syntax and Examples

  1. >>> plt.imshow(f1[320:340,510:530],,interpolation=’bilinear’)
  2. <matplotlib.image.AxesImage object at 0x01F2FB90>
  3. >>> plt.imshow(f1[320:340,510:530],,interpolation=’nearest’)
  4. <matplotlib.image.AxesImage object at 0x019954B0>

Image Processing with SciPy and NumPy — Interpolation

Basic Manipulations for Images

Let’s look at images as arrays and use numpy to handle them.

Image Processing with SciPy and NumPy — Manipulations for Images

  1. >>> face=misc.face(gray=True)
  2. >>> face[0,40]
  3. 127
  4. >>> face[10:13,20:23]
  5. array([[141, 153, 145],
  6. [133, 134, 125],
  7. [ 96, 92, 94]], dtype=uint8)
  8. >>> face[100:120]=255
  9. >>> lx,ly=face.shape
  10. >>> X,Y=np.ogrid[0:lx,0:ly]
  11. >>> mask=(X-lx/2)**2+(Y-ly/2)**2>lx*ly/4
  12. >>> face[mask]=0
  13. >>> face[range(400),range(400)]=255

This gives us the following result:

Image Processing with SciPy and NumPy

Have a Look at Python Generators and Generator Expressions in Detail

a. Statistical Information

We can use the max() and min()functions to return the maximum and minimum along a given axis. The function mean() returns the average of the array elements along given axis.

  1. >>> face=misc.face(gray=True)
  2. >>> face.mean()
  3. 113.48026784261067
  4. >>> face.max()
  5. 250
  6. >>> face.min()
  7. 0

b. Geometrical Transformations

We can rotate, crop, and flip an image using scipy.

  1. >>> face=misc.face(gray=True)
  2. >>> lx,ly=face.shape
  3. >>> crop=face[lx//4:-lx//4,ly//4:-ly//4]
  4. >>> flip=np.flipud(face)
  5. >>> from scipy import ndimage
  6. >>> rotate=ndimage.rotate(face,45)
  7. >>> rotate_noreshape=ndimage.rotate(face,45,reshape=False)

Image Processing with SciPy and NumPy — Geometrical Transformations

Read More About Errors and Exceptions in Python Programming

Filtering Images

We can also blur/smooth, sharpen, and denoise images. Through local filters, we can use a function of values of the neighboring pixels to replace the value of a pixel.

Image Processing with SciPy and NumPy — Filtering Images

a. Blurring/Smoothing Effect

We can either use a Gaussian filter or a uniform filter:

  1. >>> face=misc.face(gray=True)
  2. >>> blurred=ndimage.gaussian_filter(face,sigma=3)
  3. >>> way_blurred=ndimage.gaussian_filter(face,sigma=5)
  4. >>> local_mean=ndimage.uniform_filter(face,size=11)

Image Processing with SciPy and NumPy — Blurring/Smoothing

b. Sharpening Effect

We can sharpen a blurred image as:

  1. >>> face=misc.face(gray=True).astype(float)
  2. >>> blurred=ndimage.gaussian_filter(face,3)

Do you know Comparison Between Python Iterators and Generators

We can also add a Laplacian approximation to increase the weight of the edges:

  1. >>> filter_blurred=ndimage.gaussian_filter(blurred,1)
  2. >>> alpha=30
  3. >>> sharpened=blurred+alpha*(blurred-filter_blurred)

c. Denoising Effect

  1. >>> f=misc.face(gray=True)
  2. >>> f=f[230:290,220:320]
  3. >>> noisy=f+0.4*f.std()*np.random.random(f.shape)

This gives us a noisy face. To smoothe noise and the edges, we use a Gaussian filter:

  1. >>> gauss_denoised=ndimage.gaussian_filter(noisy,2)

To preserve the edges, we use a median filter:

  1. >>> median_denoised=ndimage.median_filter(noisy,3)

Image Processing with SciPy and NumPy — Denoising

For figures with straight boundaries and low curvature, a median filter provides a better result:

  1. >>> im=np.zeros((20,20))
  2. >>> im[5:-5,5:-5]=1
  3. >>> im=ndimage.distance_transform_bf(im)
  4. >>> im_noise=im+0.2*np.random.randn(*im.shape)
  5. >>> im_med=ndimage.median_filter(im_noise,3)

Image Processing with SciPy and NumPy — Denoising

Do You Know About Python Comment, Indentation and Statement

Extracting Features

We can also extract features in our images like detecting edges and carry out segmentation. Let’s see how.

a. Edge Detection

  1. >>> im=np.zeros((256,256))
  2. >>> im[64:-64,64:-64]=1
  3. >>> im=ndimage.rotate(im,15,mode=’constant’)
  4. >>> im=ndimage.gaussian_filter(im,8)

For high-intensity variations, we can use Sobel, a gradient operator-

Image Processing with SciPy and NumPy — Edge Detection

  1. >>> sx=ndimage.sobel(im,axis=0,mode=’constant’)
  2. >>> sy=ndimage.sobel(im,axis=1,mode=’constant’)
  3. >>> sob=np.hypot(sx,sy)

b. Segmentation

  1. >>> n,l=10,256
  2. >>> im=np.zeros((l,l))
  3. >>> np.random.seed(1)
  4. >>> points=l*np.random.random((2,n**2))
  5. >>> im[(points[0]).astype(,(points[1]).astype(]=1
  6. >>> im=ndimage.gaussian_filter(im,sigma=l/(4.*n))
  7. >>>
  8. >>> mask=(im>im.mean()).astype(np.float)
  9. >>> mask+=0.1*im
  10. >>> img=mask+0.2*np.random.randn(*mask.shape)
  11. >>>
  12. >>> hist,bin_edges=np.histogram(img,bins=60)
  13. >>> bin_centers=0.5*(bin_edges[:-1]+bin_edges[1:])
  14. >>>
  15. >>> binary_img=img>0.5

Image Processing with SciPy and NumPy — Segmentation

Let’s Explore Python Modules vs Packages

So, this was all about Image Processing with SciPy and NumPy. Hope you like our explanation.


Hence, we learned about Image Processing with SciPy and NumPy. In addition, we talked about Prerequisite for image processing, Reading and Writing to an image, manipulation in images. At last, we studied Filtering images: blurring, sharping etc. Furthermore, if you have doubt, feel free to ask in the comment section.

read original article at——artificial_intelligence-5