Python-3.4.3/C3/Image-manipulation-using-Arrays/English
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. |