|
| 1 | +# Opening RGB image as array, converting to GreyScale |
| 2 | + |
| 3 | +# Importing needed libraries |
| 4 | +import numpy as np |
| 5 | +from PIL import Image |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +from skimage import color |
| 8 | +from skimage import io |
| 9 | +import scipy.misc |
| 10 | + |
| 11 | + |
| 12 | +# Creating an array from image data |
| 13 | +image_RGB = Image.open("images/eagle.jpg") |
| 14 | +image_np = np.array(image_RGB) |
| 15 | + |
| 16 | +# Checking the type of the array |
| 17 | +print(type(image_np)) # <class 'numpy.ndarray'> |
| 18 | +# Checking the shape of the array |
| 19 | +print(image_np.shape) |
| 20 | + |
| 21 | +# Showing image with every channel separately |
| 22 | +channel_R = image_np[:, :, 0] |
| 23 | +channel_G = image_np[:, :, 1] |
| 24 | +channel_B = image_np[:, :, 2] |
| 25 | + |
| 26 | +# Creating a figure with subplots |
| 27 | +f, ax = plt.subplots(nrows=2, ncols=2) |
| 28 | +# ax is (2, 2) np array and to make it easier to read we use 'flatten' function |
| 29 | +# Or we can call each time ax[0, 0] |
| 30 | +ax0, ax1, ax2, ax3 = ax.flatten() |
| 31 | + |
| 32 | +# Adjusting first subplot |
| 33 | +ax0.imshow(channel_R, cmap='Reds') |
| 34 | +ax0.set_xlabel('') |
| 35 | +ax0.set_ylabel('') |
| 36 | +ax0.set_title('Red channel') |
| 37 | + |
| 38 | +# Adjusting second subplot |
| 39 | +ax1.imshow(channel_G, cmap='Greens') |
| 40 | +ax1.set_xlabel('') |
| 41 | +ax1.set_ylabel('') |
| 42 | +ax1.set_title('Green channel') |
| 43 | + |
| 44 | +# Adjusting third subplot |
| 45 | +ax2.imshow(channel_B, cmap='Blues') |
| 46 | +ax2.set_xlabel('') |
| 47 | +ax2.set_ylabel('') |
| 48 | +ax2.set_title('Blue channel') |
| 49 | + |
| 50 | +# Adjusting fourth subplot |
| 51 | +ax3.imshow(image_np) |
| 52 | +ax3.set_xlabel('') |
| 53 | +ax3.set_ylabel('') |
| 54 | +ax3.set_title('Original image') |
| 55 | + |
| 56 | +# Function to make distance between figures |
| 57 | +plt.tight_layout() |
| 58 | +# Giving the name to the window with figure |
| 59 | +f.canvas.set_window_title('Eagle image in three channels R, G and B') |
| 60 | +# Showing the plots |
| 61 | +plt.show() |
| 62 | + |
| 63 | + |
| 64 | +# Converting RGB image into GrayScale image |
| 65 | +# Using formula: |
| 66 | +# Y' = 0.299 R + 0.587 G + 0.114 B |
| 67 | +image_RGB = Image.open("images/eagle.jpg") |
| 68 | +image_np = np.array(image_RGB) |
| 69 | +image_GreyScale = image_np[:, :, 0] * 0.299 + image_np[:, :, 1] * 0.587 + image_np[:, :, 2] * 0.114 |
| 70 | +# Checking the type of the array |
| 71 | +print(type(image_GreyScale)) # <class 'numpy.ndarray'> |
| 72 | +# Checking the shape of the array |
| 73 | +print(image_GreyScale.shape) |
| 74 | +# Giving the name to the window with figure |
| 75 | +plt.figure('GreyScaled image from RGB') |
| 76 | +# Showing the image by using obtained array |
| 77 | +plt.imshow(image_GreyScale, cmap='Greys') |
| 78 | +plt.show() |
| 79 | +# Preparing array for saving - creating three channels with the same data in each |
| 80 | +# Firstly, creating array with zero elements |
| 81 | +# And by 'image_GreyScale.shape + tuple([3])' we add one more element '3' to the tuple |
| 82 | +# Now the shape will be (1080, 1920, 3) - which is tuple type |
| 83 | +image_GreyScale_with_3_channels = np.zeros(image_GreyScale.shape + tuple([3])) |
| 84 | +# Secondly, reshaping GreyScale image from 2D to 3D |
| 85 | +x = image_GreyScale.reshape((1080, 1920, 1)) |
| 86 | +# Finally, writing all data in three channels |
| 87 | +image_GreyScale_with_3_channels[:, :, 0] = x[:, :, 0] |
| 88 | +image_GreyScale_with_3_channels[:, :, 1] = x[:, :, 0] |
| 89 | +image_GreyScale_with_3_channels[:, :, 2] = x[:, :, 0] |
| 90 | +# Saving image into a file from obtained 3D array |
| 91 | +scipy.misc.imsave("images/result_1.jpg", image_GreyScale_with_3_channels) |
| 92 | +# Checking that image was written with three channels and they are identical |
| 93 | +result_1 = Image.open("images/result_1.jpg") |
| 94 | +result_1_np = np.array(result_1) |
| 95 | +print(result_1_np.shape) |
| 96 | +print(np.array_equal(result_1_np[:, :, 0], result_1_np[:, :, 1])) |
| 97 | +print(np.array_equal(result_1_np[:, :, 1], result_1_np[:, :, 2])) |
| 98 | +# Showing saved resulted image |
| 99 | +# Giving the name to the window with figure |
| 100 | +plt.figure('GreyScaled image from RGB') |
| 101 | +# Here we don't need to specify the map like cmap='Greys' |
| 102 | +plt.imshow(result_1_np) |
| 103 | +plt.show() |
| 104 | + |
| 105 | + |
| 106 | +# Another way to convert RGB image into GreyScale image |
| 107 | +image_RGB = io.imread("images/eagle.jpg") |
| 108 | +image_GreyScale = color.rgb2gray(image_RGB) |
| 109 | +# Checking the type of the array |
| 110 | +print(type(image_GreyScale)) # <class 'numpy.ndarray'> |
| 111 | +# Checking the shape of the array |
| 112 | +print(image_GreyScale.shape) |
| 113 | +# Giving the name to the window with figure |
| 114 | +plt.figure('GreyScaled image from RGB') |
| 115 | +# Showing the image by using obtained array |
| 116 | +plt.imshow(image_GreyScale, cmap='Greys') |
| 117 | +plt.show() |
| 118 | +# Saving converted image into a file from processed array |
| 119 | +scipy.misc.imsave("images/result_2.jpg", image_GreyScale) |
| 120 | + |
| 121 | + |
| 122 | +# One more way for converting |
| 123 | +image_RGB_as_GreyScale = io.imread("images/eagle.jpg", as_gray=True) |
| 124 | +# Checking the type of the array |
| 125 | +print(type(image_RGB_as_GreyScale)) # <class 'numpy.ndarray'> |
| 126 | +# Checking the shape of the array |
| 127 | +print(image_RGB_as_GreyScale.shape) |
| 128 | +# Giving the name to the window with figure |
| 129 | +plt.figure('GreyScaled image from RGB') |
| 130 | +# Showing the image by using obtained array |
| 131 | +plt.imshow(image_RGB_as_GreyScale, cmap='Greys') |
| 132 | +plt.show() |
| 133 | +# Saving converted image into a file from processed array |
| 134 | +scipy.misc.imsave("images/result_3.jpg", image_RGB_as_GreyScale) |
0 commit comments