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

From Script | Spoken-Tutorial
Revision as of 10:24, 14 November 2018 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Image manipulation using arrays

Author: Aditya Palaparthy, Arun KP

Keywords: Python, Ipython, imread, imshow, shape, video tutorial


Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Image manipulation using arrays.
Show Slide

Objectives


In this tutorial, you will learn to-
  • Read images into arrays
  • Perform processing on them using simple array manipulations
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Prerequisite slide

To practise this tutorial, you should know how to
  • run basic Python commands on the ipython console and
  • access parts of arrays.

If not, see the relevant Python tutorials on this website.

Slide:

Download files

Please download the files Squares.png and Python.png from the Code files link of this tutorial.


Save the files in the current working directory.

Show Squares.png First we will learn how to access parts of an array.


For this purpose, we shall use the image Squares.png present in the current working directory.

Open terminal Let us start ipython.


Open the terminal.

Type, ipython3 Type ipython3 and press Enter.


From here onwards remember to press the Enter key after typing every command on the terminal.

Type,

import matplotlib.pyplot as plt


img = plt.imread('Squares.png')


Highlight the first line


Highlight pyplot


To read an image into an array, we use the imread command.


Let us now read the data in Squares.png into the array img.


Type,

import matplotlib.pyplot as plt


Then type,

img is equal to plt.imread inside brackets inside single quotes Squares.png


In the first line pyplot is imported from matplotlib module with plt as alias.


This is to make use of methods like imread which are part of pyplot.

Type,

plt.imshow(img)

plt.show()

We can see the image by using the functions imshow followed by show.


Type,

plt.imshow inside brackets img


Then type,

plt.show open and close brackets


It displays the image in a separate popup window.


The original image is in the black and white.


But here we do not see black and white, because imshow has mapped black and white to different colors.


This can be changed by using a different color map.

Close the window Close the image window to return to the IPython console.
Type,

plt.imshow(img, cmap='gray')

plt.show()

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


Then type,

plt.show open and close brackets


Now we can see the image in black and white.

close the image Close the image now.
Type,

img

To see what has been read into img variable, type,

img


We can see that an array is displayed.

Type,


img.shape


Highlight 300x300

To check the dimensions of any array, we can use shape function.


Type,

img.shape


As we can see, we got the dimensions of the image.


The image Squares.png has the dimensions of 300 by 300.

Show Slide

Squares.png

Point at top-left quadrant of the image

Now, we wish to obtain the top left quarter of the image.


To do this, we need to access top half of the rows and left half of the columns of the array.


We know that the shape of the image is 300 by 300.


For top-left corner of the image we need first 150 rows and the first 150 columns.

Type,

img[:150, :150]


Highlight output

So type,

img inside square brackets colon 150 comma colon 150


This gives us the top-left corner of the image.

Type,

plt.imshow(img[:150,:150])

plt.show()

We use the imshow command to see the slice we obtained in the form of an image.


Type as shown.


We got the required slice now.

Close the image Close this image.
Pause the video.


Try this exercise and then resume the video.

Show Slide

Exercise 1

Obtain the square in the center of the image Squares.png with size 150 by 150.


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
Switch to terminal Switch to the terminal for the solution.
Type,

plt.imshow(img[75:225, 75:225])

plt.show()

Type as shown.


Hence, we get the center of the image.

Close the image Close this image.
Type,

img1 = img[75:225, 75:225]

plt.imshow(img1)

plt.show()

You can also save the sliced image array to a new variable.


Type,

img1 is equal to img inside square brackets 75 colon 225 comma 75 colon 225]


Then to see the image type as shown.

Close the image window Close this image window
Type,

img[::2, ::2]

We also know how to stride over an array.


For that we can drop alternate rows and columns out of the image.


Type,

img inside square brackets colon colon 2 comma colon colon 2

Type,

plt.imshow(img[::2, ::2])

plt.show()

To see this image, type as shown.


We can see that the scale has reduced.


We can observe some blurring near the edges if you scale it down further.

Close the image Close the image window to return to the IPython console.
Type,

plt.imshow(img[::4, ::4])

plt.show()

To increase the step to 4, type as shown.
Close the image Close the image window to return to the IPython console.
Show Slide Python.png We shall use the Python.png present in the current working directory.


Now we have an RGB image.

Type,

img=plt.imread('Python.png')

plt.imshow(img)

plt.show()

Let us slice the image and see what happens.


Type as shown.


We can see the image.

Close the image Close the image window to return to the IPython console.
Type,

img.shape


Highlilght the third number


Highlilght the first number

To check the dimensions of img, we can use shape function.


Type,

img.shape


The third number shows that there are 4 channels.


These are corresponding to red, green, blue, and transparency value of the image.


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.

Type,

plt.imshow(img[:,:,0])

plt.show()

Now we will slice the red channel of the array.


Type,

plt.imshow inside brackets img inside square brackets colon comma colon comma 0


Then type,

plt.show open and close brackets.


We got the required red channel of the array.

Close the image Close this image.
Show Slide

Summary slide


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learned to,

  • Read images into arrays and manipulate them.
Show Slide Evaluation Here is a self assessment question for you to solve


The shape of the variable img is (600, 600, 4).

What will be the shape of img1 if img1 is equal to img inside brackets colon colon 2 comma colon colon 4

Show Slide Solutions And the answer is, (300, 150, 4)
Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Slide: TBC FOSSEE team coordinates theh TBC project.
Show Slide Acknowledgement Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Show Slide Thank You This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Priyacst