-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGB2Gray.py
60 lines (48 loc) · 1.38 KB
/
RGB2Gray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt #imread, imsave
from .utility import file #filelister
# ### The Method to Convert RGB to Gray-scale image
def RGB_to_Gray(X):
"""
===========
RGB_to_Gray
===========
This Method Gives the Luminosity(Y) of an RGB Image.
Arguments
=========
X RGB Image as numpy.ndarray
Returns
=======
lum numpy.ndarray containing the Luminosity Image
"""
x, y, _ = X.shape
shape = (x,y)
lum = np.zeros(shape, dtype=X.dtype)
lum[:,:] = 0.299*X[:,:,0] + 0.587*X[:,:,1] + 0.114*X[:,:,2]
return lum
def RGB2Gray(path, outpath):
"""
========
RGB2Gray
========
Wrapper function to convert all Colour PNG files
from path to outpath
Arguments
=========
path String containing the path to search for PNG files
outpath String containing the path to return for output path
of processed Image
"""
#list files using utility.file submodule
file_list = file.filelister(path, outpath)
#convert each file and save it
for (i,j) in file_list:
#image read
colour_image = plt.imread(i)
#Gray image
gray_image = RGB_to_Gray(colour_image)
#image save
plt.imsave(j, gray_image, cmap='gray')
return