Difference between revisions of "Python-3.4.3/C3/Image-manipulation-using-Arrays/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''': '''Image manipulation using arrays''' '''Author: Aditya Palaparthy, Arun KP''' '''Keywords: Python, Ipython, imread, imshow, shape, video tutorial'''...")
 
 
Line 18: Line 18:
  
 
Objectives  
 
Objectives  
 
  
  
Line 25: Line 24:
 
* Read '''images''' into '''arrays'''
 
* Read '''images''' into '''arrays'''
 
* Perform processing on them using simple array manipulations  
 
* Perform processing on them using simple array manipulations  
 
 
  
 
|-
 
|-
Line 37: Line 34:
 
* '''Python 3.4.3 '''and
 
* '''Python 3.4.3 '''and
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 46: Line 41:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to  
  
* run basic '''Python '''commands on the '''ipython console '''and
+
* run basic '''Python commands''' on the '''ipython console '''and
 
* access parts of '''arrays'''.
 
* access parts of '''arrays'''.
  
Line 58: Line 53:
  
  
Save the files in the current working directory.
+
Save the files in the current '''working directory'''.
  
 
|-
 
|-
Line 65: Line 60:
  
  
For this purpose, we shall use the image '''Squares.png''' present in the current working directory.
+
For this purpose, we shall use the image '''Squares.png''' present in the current '''working directory'''.
  
 
|-
 
|-
Line 79: Line 74:
  
  
From here onwards remember to press the Enter key after typing every command on the terminal.
+
From here onwards remember to press the '''Enter''' key after typing every '''command''' on the '''terminal'''.
  
 
|-
 
|-
Line 97: Line 92:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To read an '''image''' into an array, we use the '''imread''' command.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To read an '''image''' into an '''array''', we use the '''imread command'''.  
  
  
Line 116: Line 111:
  
  
This is to make use of methods like '''imread '''which are part of '''pyplot.'''
+
This is to make use of '''methods''' like '''imread '''which are part of '''pyplot.'''
  
 
|-
 
|-
Line 124: Line 119:
  
 
'''plt.show()'''
 
'''plt.show()'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We can see the '''image''' by using the '''functions''' '''imshow''' followed by '''show'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We can see the image by using the '''functions imshow''' followed by '''show'''.  
  
  
Line 143: Line 138:
  
  
But here we do not see black and white, because '''imshow''' has '''mapped''' black and white to different '''colors'''.  
+
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'''.  
+
This can be changed by using a different '''color map'''.  
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the window
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the window
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image window to return to the IPython console.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image window to return to the '''IPython console'''.
  
 
|-
 
|-
Line 183: Line 178:
  
  
We can see that an '''array''' is displayed'''. '''
+
We can see that an '''array''' is displayed.
  
 
|-
 
|-
Line 193: Line 188:
  
 
'''Highlight 300x300 '''
 
'''Highlight 300x300 '''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To check the '''dimensions''' of any '''array''', we can use '''shape''' '''function'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To check the '''dimensions''' of any '''array''', we can use '''shape function'''.  
  
  
Line 201: Line 196:
  
  
As we can see, we got the dimensions of the '''image'''.
+
As we can see, we got the dimensions of the image.
  
  
The image''' Squares.png''' has the dimensions of 300 ''by ''300.  
+
The image''' Squares.png''' has the dimensions of 300 by 300.  
  
 
|-
 
|-
Line 215: Line 210:
  
  
To do this, we need to access top half of the '''rows''' and left half of the '''columns''' of the '''array'''.  
+
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.
+
We know that the shape of the image is 300 by 300.
  
  
Line 243: Line 238:
  
 
'''plt.show()'''
 
'''plt.show()'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We use the '''imshow''' '''command''' to see the '''slice''' we obtained in the form of an '''image.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We use the '''imshow command''' to see the slice we obtained in the form of an image.  
  
  
Line 273: Line 268:
 
* rows from one-fourth to three-fourth of the rows i.e. from '''75''' to '''225''' and  
 
* 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'''
 
* columns from one-fourth to three-fourth of the columns, i.e. from '''75''' to '''225'''
 
 
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to terminal
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal for the solution.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution.  
  
 
|-
 
|-
Line 286: Line 279:
  
 
'''plt.show()'''
 
'''plt.show()'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Type as shown.
  
  
 
+
Hence, we get the center of the image.  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Type as shown'''.'''
+
 
+
 
+
Hence, we get the '''center''' of the '''image. '''
+
  
 
|-
 
|-
Line 306: Line 296:
  
 
'''plt.show()'''
 
'''plt.show()'''
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| You can also save the sliced image array to a new variable.
+
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| You can also save the sliced image '''array''' to a new variable.
  
  
Line 327: Line 317:
  
  
For that we can drop '''alternate''' '''rows''' and '''columns''' out of the '''image'''.  
+
For that we can drop alternate rows and columns out of the image.  
  
  
Line 343: Line 333:
  
  
We can see that the '''scale''' has reduced.  
+
We can see that the scale has reduced.  
  
  
Line 350: Line 340:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image window to return to the IPython console.
+
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image window to return to the '''IPython console'''.
  
 
|-
 
|-
Line 359: Line 349:
 
'''plt.show() '''
 
'''plt.show() '''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To increase the '''step''' to 4, type as shown.
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To increase the '''step''' to 4, type as shown.
 
 
 
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image window to return to the IPython console.
+
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image window to return to the '''IPython console'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide Python.png
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide Python.png
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We shall use the Python.png present in the current working directory.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We shall use the '''Python.png''' present in the current '''working directory'''.
  
  
Line 392: Line 379:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image window to return to the IPython console
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Close the image window to return to the '''IPython console'''.
  
 
|-
 
|-
Line 404: Line 391:
  
 
Highlilght the first number
 
Highlilght the first number
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To check the '''dimensions''' of '''img''', we can use '''shape''' '''function'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To check the dimensions of '''img''', we can use '''shape function'''.  
  
  
Line 429: Line 416:
  
 
'''plt.show()'''
 
'''plt.show()'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now we will slice the red channel of the array.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now we will slice the red channel of the '''array'''.
  
  
Line 442: Line 429:
  
  
We got the required red channel of the array.
+
We got the required red channel of the '''array'''.
  
 
|-
 
|-
Line 458: Line 445:
  
  
In this tutorial, we have learned to,''' '''
+
In this tutorial, we have learned to,
  
 
* Read '''images''' into '''arrays''' and manipulate them.  
 
* Read '''images''' into '''arrays''' and manipulate them.  
 
 
  
 
|-
 
|-

Latest revision as of 10:24, 14 November 2018

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