Skip to content

Commit 6cf108d

Browse files
Add files via upload
1 parent 19154a7 commit 6cf108d

9 files changed

+170
-0
lines changed

Converting_RGB_to_GreyScale.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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)

Opening_png_jpg.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Testing to open images and applying simple filtering
2+
3+
import tkinter as tk
4+
from PIL import Image, ImageTk
5+
6+
# Option 1 - opening .png images
7+
# Establishing 'root' as the 'Tk' window
8+
root = tk.Tk()
9+
# Uploading image from file
10+
image_1 = tk.PhotoImage(file="images/eagle.png")
11+
# Creating a 'Label' object that contains uploaded image
12+
label = tk.Label(root, image=image_1)
13+
# Packing 'Label' into 'Tk' window
14+
label.pack()
15+
# Setting the title
16+
root.title('Opening .png image')
17+
# root.geometry("800x600")
18+
# Starts loop with event
19+
root.mainloop()
20+
21+
22+
# Option 2 - opening .jpg images
23+
# Establishing 'root' as the 'Tk' window
24+
root = tk.Tk()
25+
# Uploading image from file
26+
image_2 = Image.open("images/eagle.jpg")
27+
# Creating a 'Label' object that contains uploaded image
28+
label = tk.Label(root)
29+
label.image = ImageTk.PhotoImage(image_2)
30+
label['image'] = label.image
31+
# Packing 'Label' into 'Tk' window
32+
label.pack()
33+
# Setting the title
34+
root.title('Opening .jpg image')
35+
# Starts loop with event
36+
root.mainloop()

images/RGB_channels.png

272 KB
Loading

images/eagle.jpg

238 KB
Loading

images/eagle.png

1.41 MB
Loading

images/eagle_greyscale.jpg

175 KB
Loading

images/result_1.jpg

143 KB
Loading

images/result_2.jpg

132 KB
Loading

images/result_3.jpg

132 KB
Loading

0 commit comments

Comments
 (0)