-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep03_GrayscalePreProcess.py
49 lines (40 loc) · 1.65 KB
/
step03_GrayscalePreProcess.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
#reference: https://bit.ly/3vku3bt
import os
import sys
from PIL import Image, ImageOps
def ConvertImagesToGrayscale(colorDir="", grayDir=""):
if not os.path.exists(colorDir):
print('No RBG image directory found.')
return
os.makedirs(os.path.dirname(grayDir), exist_ok=True)
numColored = len(os.listdir(colorDir))
if numColored < 1:
print('No images in cropped_frames (i.e. RBG image) directory.')
return
for i,infile in enumerate(os.listdir(colorDir)):
print(int(i/numColored*100), '%', end='\r')
outfile = grayDir + infile
try:
im = Image.open(colorDir+infile)
gray_im = ImageOps.grayscale(im)
gray_im.save(outfile, "png")
except IOError as e:
print(e,"ERROR - failed to convert '%s'" % infile)
print("100%")
def LaunchConvert(subdirectories = True, inpath = "cropped_frames/",outpath = "gray_frames/"):
print("Converting...")
if subdirectories:
dirs = os.listdir(inpath)
for d in dirs:
print("Converting image files in directory:", inpath+d+'/')
ConvertImagesToGrayscale(inpath+d+'/', outpath+d+'/')
else:
ConvertImagesToGrayscale(inpath, outpath)
args = sys.argv
if len(args) < 2:
LaunchConvert(subdirectories = True, inpath = "cropped_frames/",outpath = "gray_frames/")
elif len(args) < 4:
print("Not enough arguments. Please provide: 1)subdirectories (boolean), 2)inpath (relative to working directory, ending in /), 3)outpath (ending in /)")
else:
LaunchConvert(subdirectories = True if args[1]=="True" else False,
inpath = args[2], outpath = args[3])