Skip to content

Commit b38b9e3

Browse files
Add files via upload
1 parent 7d8c9e8 commit b38b9e3

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

Codes/Converting_RGB_to_GreyScale.py

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

Codes/Opening_png_jpg.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# File: Opening_png_jpg.py
2+
# Description: Testing to open images of different format
3+
# Environment: PyCharm and Anaconda environment
4+
#
5+
# MIT License
6+
# Copyright (c) 2018 Valentyn N Sichkar
7+
# github.com/sichkar-valentyn
8+
#
9+
# Reference to:
10+
# Valentyn N Sichkar. Image processing in Python // GitHub platform. DOI: 10.5281/zenodo.1343603
11+
12+
13+
14+
15+
# Testing to open images of different format
16+
17+
import tkinter as tk
18+
from PIL import Image, ImageTk
19+
20+
# Option 1 - opening .png images
21+
# Establishing 'root' as the 'Tk' window
22+
root = tk.Tk()
23+
# Uploading image from file
24+
image_1 = tk.PhotoImage(file="images/eagle.png")
25+
# Creating a 'Label' object that contains uploaded image
26+
label = tk.Label(root, image=image_1)
27+
# Packing 'Label' into 'Tk' window
28+
label.pack()
29+
# Setting the title
30+
root.title('Opening .png image')
31+
# root.geometry("800x600")
32+
# Starts loop with event
33+
root.mainloop()
34+
35+
36+
# Option 2 - opening .jpg images
37+
# Establishing 'root' as the 'Tk' window
38+
root = tk.Tk()
39+
# Uploading image from file
40+
image_2 = Image.open("images/eagle.jpg")
41+
# Creating a 'Label' object that contains uploaded image
42+
label = tk.Label(root)
43+
label.image = ImageTk.PhotoImage(image_2)
44+
label['image'] = label.image
45+
# Packing 'Label' into 'Tk' window
46+
label.pack()
47+
# Setting the title
48+
root.title('Opening .jpg image')
49+
# Starts loop with event
50+
root.mainloop()

0 commit comments

Comments
 (0)