-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgray_multiple.py
More file actions
31 lines (26 loc) · 864 Bytes
/
gray_multiple.py
File metadata and controls
31 lines (26 loc) · 864 Bytes
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
import cv2
import os,glob
from os import listdir,makedirs
from os.path import isfile,join
path = 'resized/' # Source Folder
dstpath = 'gray/' # Destination Folder
try:
makedirs(dstpath)
except:
print ("The directory already exists, and the images will be written in the same folder")
files = [f for f in listdir(path) if isfile(join(path,f))]
for image in files:
try:
img = cv2.imread(os.path.join(path,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dstPath = join(dstpath,image)
cv2.imwrite(dstPath,gray)
except:
print ("{} is not converted".format(image))
for fil in glob.glob("*.png"):
try:
image = cv2.imread(fil)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join(dstpath,fil),gray_image)
except:
print('{} is not converted')