# File: Converting_RGB_to_GreyScale.py
# Description: Opening RGB image as array, converting to GreyScale and saving result into new file
# Environment: PyCharm and Anaconda environment
#
# MIT License
# Copyright (c) 2018 Valentyn N Sichkar
# github.com/sichkar-valentyn
#
# Reference to:
# Valentyn N Sichkar. Image processing in Python // GitHub platform. DOI: 10.5281/zenodo.1343603




# Opening RGB image as array, converting to GreyScale and saving result into new file

# Importing needed libraries
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from skimage import color
from skimage import io
import scipy.misc


# Creating an array from image data
image_RGB = Image.open("images/eagle.jpg")
image_np = np.array(image_RGB)

# Checking the type of the array
print(type(image_np))  # <class 'numpy.ndarray'>
# Checking the shape of the array
print(image_np.shape)

# Showing image with every channel separately
channel_R = image_np[:, :, 0]
channel_G = image_np[:, :, 1]
channel_B = image_np[:, :, 2]

# Creating a figure with subplots
f, ax = plt.subplots(nrows=2, ncols=2)
# ax is (2, 2) np array and to make it easier to read we use 'flatten' function
# Or we can call each time ax[0, 0]
ax0, ax1, ax2, ax3 = ax.flatten()

# Adjusting first subplot
ax0.imshow(channel_R, cmap='Reds')
ax0.set_xlabel('')
ax0.set_ylabel('')
ax0.set_title('Red channel')

# Adjusting second subplot
ax1.imshow(channel_G, cmap='Greens')
ax1.set_xlabel('')
ax1.set_ylabel('')
ax1.set_title('Green channel')

# Adjusting third subplot
ax2.imshow(channel_B, cmap='Blues')
ax2.set_xlabel('')
ax2.set_ylabel('')
ax2.set_title('Blue channel')

# Adjusting fourth subplot
ax3.imshow(image_np)
ax3.set_xlabel('')
ax3.set_ylabel('')
ax3.set_title('Original image')

# Function to make distance between figures
plt.tight_layout()
# Giving the name to the window with figure
f.canvas.set_window_title('Eagle image in three channels R, G and B')
# Showing the plots
plt.show()


# Converting RGB image into GrayScale image
# Using formula:
# Y' = 0.299 R + 0.587 G + 0.114 B
image_RGB = Image.open("images/eagle.jpg")
image_np = np.array(image_RGB)
image_GreyScale = image_np[:, :, 0] * 0.299 + image_np[:, :, 1] * 0.587 + image_np[:, :, 2] * 0.114
# Checking the type of the array
print(type(image_GreyScale))  # <class 'numpy.ndarray'>
# Checking the shape of the array
print(image_GreyScale.shape)
# Giving the name to the window with figure
plt.figure('GreyScaled image from RGB')
# Showing the image by using obtained array
plt.imshow(image_GreyScale, cmap='Greys')
plt.show()
# Preparing array for saving - creating three channels with the same data in each
# Firstly, creating array with zero elements
# And by 'image_GreyScale.shape + tuple([3])' we add one more element '3' to the tuple
# Now the shape will be (1080, 1920, 3) - which is tuple type
image_GreyScale_with_3_channels = np.zeros(image_GreyScale.shape + tuple([3]))
# Secondly, reshaping GreyScale image from 2D to 3D
x = image_GreyScale.reshape((1080, 1920, 1))
# Finally, writing all data in three channels
image_GreyScale_with_3_channels[:, :, 0] = x[:, :, 0]
image_GreyScale_with_3_channels[:, :, 1] = x[:, :, 0]
image_GreyScale_with_3_channels[:, :, 2] = x[:, :, 0]
# Saving image into a file from obtained 3D array
scipy.misc.imsave("images/result_1.jpg", image_GreyScale_with_3_channels)
# Checking that image was written with three channels and they are identical
result_1 = Image.open("images/result_1.jpg")
result_1_np = np.array(result_1)
print(result_1_np.shape)
print(np.array_equal(result_1_np[:, :, 0], result_1_np[:, :, 1]))
print(np.array_equal(result_1_np[:, :, 1], result_1_np[:, :, 2]))
# Showing saved resulted image
# Giving the name to the window with figure
plt.figure('GreyScaled image from RGB')
# Here we don't need to specify the map like cmap='Greys'
plt.imshow(result_1_np)
plt.show()


# Another way to convert RGB image into GreyScale image
image_RGB = io.imread("images/eagle.jpg")
image_GreyScale = color.rgb2gray(image_RGB)
# Checking the type of the array
print(type(image_GreyScale))  # <class 'numpy.ndarray'>
# Checking the shape of the array
print(image_GreyScale.shape)
# Giving the name to the window with figure
plt.figure('GreyScaled image from RGB')
# Showing the image by using obtained array
plt.imshow(image_GreyScale, cmap='Greys')
plt.show()
# Saving converted image into a file from processed array
scipy.misc.imsave("images/result_2.jpg", image_GreyScale)


# One more way for converting
image_RGB_as_GreyScale = io.imread("images/eagle.jpg", as_gray=True)
# Checking the type of the array
print(type(image_RGB_as_GreyScale))  # <class 'numpy.ndarray'>
# Checking the shape of the array
print(image_RGB_as_GreyScale.shape)
# Giving the name to the window with figure
plt.figure('GreyScaled image from RGB')
# Showing the image by using obtained array
plt.imshow(image_RGB_as_GreyScale, cmap='Greys')
plt.show()
# Saving converted image into a file from processed array
scipy.misc.imsave("images/result_3.jpg", image_RGB_as_GreyScale)

[((27, 12, 27, 42), 'PIL.Image.open', 'Image.open', ({(27, 23, 27, 41): '"""images/eagle.jpg"""'}, {}), "('images/eagle.jpg')", False, 'from PIL import Image\n'), ((28, 11, 28, 30), 'numpy.array', 'np.array', ({(28, 20, 28, 29): 'image_RGB'}, {}), '(image_RGB)', True, 'import numpy as np\n'), ((41, 8, 41, 38), 'matplotlib.pyplot.subplots', 'plt.subplots', (), '', True, 'import matplotlib.pyplot as plt\n'), ((71, 0, 71, 18), 'matplotlib.pyplot.tight_layout', 'plt.tight_layout', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((75, 0, 75, 10), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((81, 12, 81, 42), 'PIL.Image.open', 'Image.open', ({(81, 23, 81, 41): '"""images/eagle.jpg"""'}, {}), "('images/eagle.jpg')", False, 'from PIL import Image\n'), ((82, 11, 82, 30), 'numpy.array', 'np.array', ({(82, 20, 82, 29): 'image_RGB'}, {}), '(image_RGB)', True, 'import numpy as np\n'), ((89, 0, 89, 39), 'matplotlib.pyplot.figure', 'plt.figure', ({(89, 11, 89, 38): '"""GreyScaled image from RGB"""'}, {}), "('GreyScaled image from RGB')", True, 'import matplotlib.pyplot as plt\n'), ((91, 0, 91, 41), 'matplotlib.pyplot.imshow', 'plt.imshow', (), '', True, 'import matplotlib.pyplot as plt\n'), ((92, 0, 92, 10), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((107, 11, 107, 44), 'PIL.Image.open', 'Image.open', ({(107, 22, 107, 43): '"""images/result_1.jpg"""'}, {}), "('images/result_1.jpg')", False, 'from PIL import Image\n'), ((108, 14, 108, 32), 'numpy.array', 'np.array', ({(108, 23, 108, 31): 'result_1'}, {}), '(result_1)', True, 'import numpy as np\n'), ((114, 0, 114, 39), 'matplotlib.pyplot.figure', 'plt.figure', ({(114, 11, 114, 38): '"""GreyScaled image from RGB"""'}, {}), "('GreyScaled image from RGB')", True, 'import matplotlib.pyplot as plt\n'), ((116, 0, 116, 23), 'matplotlib.pyplot.imshow', 'plt.imshow', ({(116, 11, 116, 22): 'result_1_np'}, {}), '(result_1_np)', True, 'import matplotlib.pyplot as plt\n'), ((117, 0, 117, 10), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((121, 12, 121, 41), 'skimage.io.imread', 'io.imread', ({(121, 22, 121, 40): '"""images/eagle.jpg"""'}, {}), "('images/eagle.jpg')", False, 'from skimage import io\n'), ((122, 18, 122, 43), 'skimage.color.rgb2gray', 'color.rgb2gray', ({(122, 33, 122, 42): 'image_RGB'}, {}), '(image_RGB)', False, 'from skimage import color\n'), ((128, 0, 128, 39), 'matplotlib.pyplot.figure', 'plt.figure', ({(128, 11, 128, 38): '"""GreyScaled image from RGB"""'}, {}), "('GreyScaled image from RGB')", True, 'import matplotlib.pyplot as plt\n'), ((130, 0, 130, 41), 'matplotlib.pyplot.imshow', 'plt.imshow', (), '', True, 'import matplotlib.pyplot as plt\n'), ((131, 0, 131, 10), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((137, 25, 137, 68), 'skimage.io.imread', 'io.imread', (), '', False, 'from skimage import io\n'), ((143, 0, 143, 39), 'matplotlib.pyplot.figure', 'plt.figure', ({(143, 11, 143, 38): '"""GreyScaled image from RGB"""'}, {}), "('GreyScaled image from RGB')", True, 'import matplotlib.pyplot as plt\n'), ((145, 0, 145, 48), 'matplotlib.pyplot.imshow', 'plt.imshow', (), '', True, 'import matplotlib.pyplot as plt\n'), ((146, 0, 146, 10), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((110, 6, 110, 64), 'numpy.array_equal', 'np.array_equal', ({(110, 21, 110, 41): 'result_1_np[:, :, (0)]', (110, 43, 110, 63): 'result_1_np[:, :, (1)]'}, {}), '(result_1_np[:, :, (0)], result_1_np[:, :, (1)])', True, 'import numpy as np\n'), ((111, 6, 111, 64), 'numpy.array_equal', 'np.array_equal', ({(111, 21, 111, 41): 'result_1_np[:, :, (1)]', (111, 43, 111, 63): 'result_1_np[:, :, (2)]'}, {}), '(result_1_np[:, :, (1)], result_1_np[:, :, (2)])', True, 'import numpy as np\n')]