In this post, we look into some demos related to manipulating images such as:
- Read an image in RGB mode and display it
- Display pixel values in the RGB mode image
- Convert it into gray image
- Get part of the image and display it
- And finally, using the "thresholding" to convert the gray image into white/black image
In [2]:
from tabulate import tabulate
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image
Read an image¶
- We first open an image and get the 3D array for the 3 channels: Red, Green, and Blue.
- As we can see below, each channel is in the shape of $558 * 1024$
In [3]:
fname = 'Fuzhou-free-to-use.jpg'
image = Image.open(fname)
# 3D array for RGB
arr = np.asarray(image)
print(arr.shape)
plt.imshow(arr)
plt.show()
(558, 1024, 3)
Pixel values¶
- We can use the tablulate library to print out the pixel values in a channel (e.g., Green channel below)
In [4]:
arr.max(), arr.min()
table = tabulate(arr[:10,:10,1], tablefmt="fancy_grid")
print(table)
╒════╤════╤════╤════╤════╤════╤════╤════╤════╤════╕ │ 96 │ 96 │ 96 │ 96 │ 96 │ 97 │ 97 │ 97 │ 97 │ 97 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 96 │ 96 │ 96 │ 96 │ 96 │ 97 │ 97 │ 97 │ 97 │ 97 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 96 │ 96 │ 96 │ 96 │ 97 │ 97 │ 97 │ 97 │ 98 │ 98 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 96 │ 96 │ 96 │ 96 │ 97 │ 97 │ 97 │ 97 │ 98 │ 98 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 96 │ 96 │ 96 │ 96 │ 97 │ 97 │ 97 │ 97 │ 98 │ 98 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 96 │ 96 │ 96 │ 97 │ 97 │ 97 │ 97 │ 97 │ 98 │ 98 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 97 │ 97 │ 97 │ 97 │ 97 │ 97 │ 97 │ 97 │ 98 │ 98 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 97 │ 97 │ 97 │ 97 │ 97 │ 97 │ 97 │ 97 │ 98 │ 98 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 96 │ 97 │ 97 │ 97 │ 98 │ 98 │ 98 │ 98 │ 98 │ 98 │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 96 │ 97 │ 97 │ 97 │ 98 │ 98 │ 98 │ 98 │ 98 │ 98 │ ╘════╧════╧════╧════╧════╧════╧════╧════╧════╧════╛
Pixel values¶
- We can also explore the pixel values for a specific line of an image as below
- We are looking at 5th line of the figure, which is kind of near blue color
- So we can observe that, we have highest blue values followed by green, with nearly zero red values
In [5]:
r_arr = arr[:,:,0]
g_arr = arr[:,:,1]
b_arr = arr[:,:,2]
plt.plot(np.arange(1024), r_arr[5:6,:][0], c='r')
plt.plot(np.arange(1024), g_arr[5:6,:][0], c='g')
plt.plot(np.arange(1024), b_arr[5:6,:][0], c='b')
plt.show()
To gray image¶
- We can also open an image as a gray image
In [6]:
image = Image.open(fname).convert("L")
plt.imshow(arr, cmap='gray')
arr = np.asarray(image)
print(arr.shape)
plt.show()
(558, 1024)
Part of the image¶
- And grab part of the image and render it by selecting specific range of the array of image
In [7]:
plt.imshow(arr[:, 400:500], cmap='gray')
plt.show()
plt.plot(arr[:, 400:500])
plt.show()
Thresholding¶
- Finally, it is straighforward to show the thresholding concept in multimedia to convert the image into black/white image
In [8]:
plt.imshow(arr>127, cmap='gray')
plt.show()
In [ ]: