tensorflow.python.framework.errors_impl.InvalidArgumentError: image_size must contain 3 elements[4]
出现了很多非RGB格式的图片。
解决方法:
由于该类图片较多,所以选择将它们转化为RGB格式之后再储存。
#encoding:utf-8
from PIL import Image
import os
import matplotlib.pyplot as plt
def get_not_rgb_images(rootdir):
list = os.listdir(rootdir)
l = len(list)
count = 0
for i in range(0, len(list)):
filename = os.path.join(rootdir, list[i])
# print(filename)
if os.path.isfile(filename):
img = Image.open(filename)
pixels = img.getpixel((0, 0))
if type(pixels) == int:
print('单通道:' + filename)
count += 1
print('The ',i,'st pic:',count,'/',l,'\r')
#img = Image.open(imgName)
img = img.convert('RGB')
img.save(filename)
elif type(pixels) == tuple:
if len(pixels) != 3:
print('非RGB的多通道:' +filename)
count += 1
print('The ',i,'st pic:',count,'/',l,'\r')
img = img.convert('RGB')
img.save(filename)
else:
get_not_rgb_images(filename)
if __name__ == '__main__':
rootdir = '/yourfile'#您图片所在的文件夹
get_not_rgb_images(rootdir )
代码参考:
tensorflow入门笔记(二十)使用slim模型库训练自己的数据
Python图像处理库PIL中图像格式转换(二)
用python简单处理图片(1):打开\显示\保存图像