将批量图片进行翻转

在扩充数据集时,需要用到图片翻转的操作,下面是对一个数据集中的批量数据进行翻转并存储到另一个文件夹中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import cv2
import os

def read_path(file_pathname):
for filename in os.listdir(file_pathname):
print(filename)
img = cv2.imread(file_pathname+'/' + filename)
####change to gray
#(下面第一行是将RGB转成单通道灰度图,第二步是将单通道灰度图转成3通道灰度图)
img1 = cv2.flip(img, 1)
# img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# image_np=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
cv2.imwrite('E:/data/MNIST/test_flip' + "/" + filename, img1)


path = "E:/data/MNIST/test"
read_path(path) # 读取路径