python cv2 batch grayscale pictures and save the example

  • 2021-01-25 07:45:24
  • OfStack

This is as follows:


import cv2
# Circulate the grayscale image and save 
def grayImg():
 for x in range(1,38):
  # Read the pictures 
  img = cv2.imread("C:\\Users\\lyl\\Desktop\\new_dahuoji\\img2\\{}.jpg".format(str(x)))
  GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  # Save the new image after greyscale 
  cv2.imwrite("C:\\Users\\lyl\\Desktop\\1\\myProject\\pictures\\dataset2\\dahuomiao\\{}.jpg".format(str(x)),GrayImage)
grayImg()

Related articles: