convert_to_rgb.py 931 B

123456789101112131415161718192021222324252627282930
  1. import cv2
  2. import os,glob
  3. from os import listdir,makedirs
  4. from os.path import isfile,join
  5. path = 'barcode_coco' # Source Folder
  6. dstpath = 'barcode_coco_new' # Destination Folder
  7. try:
  8. makedirs(dstpath)
  9. except:
  10. print ("Directory already exist, images will be written in same folder")
  11. # Folder won't used
  12. files = list(filter(lambda f: isfile(join(path,f)), listdir(path)))
  13. for image in files:
  14. try:
  15. img = cv2.imread(os.path.join(path,image))
  16. gray = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
  17. dstPath = join(dstpath,image)
  18. cv2.imwrite(dstPath,gray)
  19. except:
  20. print ("{} is not converted".format(image))
  21. for fil in glob.glob("*.png"):
  22. try:
  23. image = cv2.imread(fil)
  24. gray_image = cv2.cvtColor(os.path.join(path,image), cv2.COLOR_BGR2GRAY) # convert to greyscale
  25. cv2.imwrite(os.path.join(dstpath,fil),gray_image)
  26. except:
  27. print('{} is not converted')