Last active
October 25, 2022 17:55
-
-
Save mattsegura/81ea25c8f0fd36aa2741c94a5060034f to your computer and use it in GitHub Desktop.
Convert images in a folder into greyscale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import os,glob | |
from os import listdir,makedirs | |
from os.path import isfile,join | |
path = '' # folder you want to read from | |
dstpath = '' # folder you want to output results | |
try: | |
makedirs(dstpath) | |
except: | |
print ("Directory already exist, images will be written in same folder") | |
# Folder won't used | |
files = list(filter(lambda f: isfile(join(path,f)), listdir(path))) | |
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("*.jpg"): | |
try: | |
image = cv2.imread(fil) | |
gray_image = cv2.cvtColor(os.path.join(path,image), cv2.COLOR_BGR2GRAY) # convert to greyscale | |
cv2.imwrite(os.path.join(dstpath,fil),gray_image) | |
except: | |
print('{} is not converted') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment