Difference between revisions of "Python-3.4.3/C3/Image-manipulation-using-Arrays/English"
(Created page with "'''Title of script''': '''Image manipulation using arrays''' '''Author: Aditya Palaparthy, Arun KP''' '''Keywords: Python, Ipython, imread, imshow, shape, video tutorial'''...") |
Nancyvarkey (Talk | contribs) |
||
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 ''' | + | * 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''' | + | | 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 | + | | 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 | + | 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 | + | 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 | + | | 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 | + | As we can see, we got the dimensions of the image. |
− | The image''' Squares.png''' has the dimensions of 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 | + | 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 | + | 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 | + | | 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. | |
− | + | ||
− | + | ||
− | + | ||
− | Hence, we get the | + | |
|- | |- | ||
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 | + | For that we can drop alternate rows and columns out of the image. |
Line 343: | Line 333: | ||
− | We can see that the | + | 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 | + | | 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
|
|
Show Slide title | Welcome to the spoken tutorial on Image manipulation using arrays. |
Show Slide
Objectives
|
In this tutorial, you will learn to-
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Prerequisite slide |
To practise this tutorial, you should know how to
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.
|
Show Squares.png | First we will learn how to access parts of an array.
|
Open terminal | Let us start ipython.
|
Type, ipython3 | Type ipython3 and press Enter.
|
Type,
import matplotlib.pyplot as plt
|
To read an image into an array, we use the imread command.
import matplotlib.pyplot as plt
img is equal to plt.imread inside brackets inside single quotes Squares.png
|
Type,
plt.imshow(img) plt.show() |
We can see the image by using the functions imshow followed by show.
plt.imshow inside brackets img
plt.show open and close brackets
|
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
plt.show open and close brackets
|
close the image | Close the image now. |
Type,
img |
To see what has been read into img variable, type,
img
|
Type,
|
To check the dimensions of any array, we can use shape function.
img.shape
|
Show Slide
Squares.png Point at top-left quadrant of the image |
Now, we wish to obtain the top left quarter of the image.
|
Type,
img[:150, :150]
|
So type,
img inside square brackets colon 150 comma colon 150
|
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.
|
Close the image | Close this image. |
Pause the video.
| |
Show Slide
Exercise 1 |
Obtain the square in the center of the image Squares.png with size 150 by 150.
|
Switch to terminal | Switch to the terminal for the solution. |
Type,
plt.imshow(img[75:225, 75:225]) plt.show() |
Type as shown.
|
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.
img1 is equal to img inside square brackets 75 colon 225 comma 75 colon 225]
|
Close the image window | Close this image window |
Type,
img[::2, ::2] |
We also know how to stride over an array.
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.
|
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.
|
Type,
img=plt.imread('Python.png') plt.imshow(img) plt.show() |
Let us slice the image and see what happens.
|
Close the image | Close the image window to return to the IPython console. |
Type,
img.shape
|
To check the dimensions of img, we can use shape function.
img.shape
|
Type,
plt.imshow(img[:,:,0]) plt.show() |
Now we will slice the red channel of the array.
plt.imshow inside brackets img inside square brackets colon comma colon comma 0
plt.show open and close brackets.
|
Close the image | Close this image. |
Show Slide
Summary slide
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide Evaluation | Here is a self assessment question for you to solve
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. |