最近在做基于tensorflow目标检测API的目标识别,如果不能事先找出图片中的非RGB图片,则会报错,下面程序实现找出路径下图片中是否含有非RGB的图片
from PIL import Image
import os
path = 'D:\\AI\project\\Tensorflow object detection\\Tensorflow object detection API\\models-master\\research\\object_detection\\images2\\test\\' # 图片目录
for file in os.listdir(path):
extension = file.split('.')[-1]
if extension == 'jpg':
fileLoc = path + file
img = Image.open(fileLoc)
if img.mode != 'RGB':
print(file + ', ' + img.mode)
elif extension == 'png':
fileLoc = path + file
img = Image.open(fileLoc)
if img.mode != 'RGB':
print(file + ', ' + img.mode)
elif extension == 'PNG':
fileLoc = path + file
img = Image.open(fileLoc)
if img.mode != 'RGB':
print(file + ', ' + img.mode)
elif extension == 'JPG':
fileLoc = path + file
img = Image.open(fileLoc)
if img.mode != 'RGB':
print(file + ', ' + img.mode)
print('Done')