Python-3.4.3/C3/Image-manipulation-using-Arrays/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Image manipulation using arrays.
00:07 In this tutorial, you will learn to -

Read images into arrays

Perform processing on them using simple array manipulations

00:19 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

Python 3.4.3 and

IPython 5.1.0

00:35 To practise this tutorial, you should know how to run basic Python commands on the ipython console and access parts of arrays.
00:46 If not, see the relevant Python tutorials on this website.
00:51 Please download the files Squares.png and Python.png from the Code files link of this tutorial.
01:00 Save the files in the current working directory.
01:04 First we will learn how to access parts of an array.
01:09 For this purpose, we shall use the image Squares.png present in the current working directory.
01:16 Let us start ipython. Open the terminal.
01:21 Type ipython3 and press Enter.
01:26 From here onwards remember to press the Enter key after typing every command on the terminal.
01:33 To read an image into an array, we use the imread command.
01:38 Let us now read the data in Squares.png into the array img.
01:44 Type, import matplotlib.pyplot as plt
01:51 Then type, img is equal to plt.imread inside brackets inside single quotes Squares.png
02:02 In the first line pyplot is imported from matplotlib module with plt as alias.
02:09 This is to make use of methods like imread which are part of pyplot.
02:15 We can see the image by using the functions imshow followed by show.
02:21 Type, plt.imshow inside brackets img Then type, plt.show open and close brackets
02:33 It displays the image in a separate popup window.The original image is in the black and white.
02:41 But here we do not see black and white, because imshow has mapped black and white to different colors.
02:49 This can be changed by using a different color map.
02:53 Close the image window to return to the IPython console.
02:58 In order to see the original black and white image type, plt.imshow inside brackets img comma cmap is equal to inside single quotes gray
03:13 Then type, plt.show open and close brackets. Now we can see the image in black and white.
03:22 Close the image now.
03:24 To see what has been read into img variable, type,img. We can see that an array is displayed.
03:34 To check the dimensions of any array, we can use shape function. Type, img.shape
03:44 As we can see, we got the dimensions of the image. The image Squares.png has the dimensions of 300 by 300.
03:54 Now, we wish to obtain the top left quarter of the image.
03:59 To do this, we need to access top half of the rows and left half of the columns of the array.
04:06 We know that the shape of the image is 300 by 300.
04:11 For top-left corner of the image we need first 150 rows and the first 150 columns.
04:19 So type, img inside square brackets colon 150 comma colon 150
04:27 This gives us the top-left corner of the image.
04:32 We use the imshow command to see the slice we obtained in the form of an image. Type as shown.
04:42 We got the required slice now. Close this image.
04:48 Pause the video. Try this exercise and then resume the video.
04:54 Obtain the square in the center of the image Squares.png with size 150 by 150.
05:02 Since we need to get center of the image, we take rows from one-fourth to three-fourth of the rows i.e. from 75 to 225 and columns from one-fourth to three-fourth of the columns, i.e. from 75 to 225
05:21 Switch to the terminal for the solution.
05:25 Type as shown. Hence, we get the center of the image. Close this image.
05:34 You can also save the sliced image array to a new variable.
05:39 Type, img1 is equal to img inside square brackets 75 colon 225 comma 75 colon 225] Then to see the image type as shown.
05:56 Close this image window
05:59 We also know how to stride over an array.
06:03 For that we can drop alternate rows and columns out of the image.
06:08 Type, img inside square brackets colon colon 2 comma colon colon 2
06:16 To see this image, type as shown. We can see that the scale has reduced.
06:24 We can observe some blurring near the edges if you scale it down further.
06:30 Close the image window to return to the IPython console.
06:35 To increase the step to 4, type as shown.
06:41 Close the image window to return to the IPython console.
06:45 We shall use the Python.png present in the current working directory.Now we have an RGB image.
06:54 Let us slice the image and see what happens.
06:58 Type as shown. We can see the image.
07:03 Close the image window to return to the IPython console.
07:07 To check the dimensions of img, we can use shape function.
07:12 Type, img.shape
07:16 The third number shows that there are 4 channels. These are corresponding to red, green, blue, and transparency value of the image.
07:28 The first number shows that the height of the image is 600. The second number shows that the width of the image is also 600.
07:39 Now we will slice the red channel of the array.
07:43 Type, plt.imshow inside brackets img inside square brackets colon comma colon comma 0
07:53 Then type, plt.show open and close brackets.
08:00 We got the required red channel of the array.
08:04 Close this image.
08:06 This brings us to the end of this tutorial. Let us summarize.
08:12 In this tutorial, we have learned to,

Read images into arrays and manipulate them.

08:19 Here is a self assessment question for you to solve
08:23 The shape of the variable img is (600, 600, 4).
08:29 What will be the shape of img1 if img1 is equal to img inside brackets colon colon 2 comma colon colon 4
08:39 And the answer is, (300, 150, 4)
08:45 Please post your timed queries in this forum.
08:49 Please post your general queries on Python in this forum.
08:54 FOSSEE team coordinates the TBC project.
08:58 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.
09:07 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Pratik kamble