tensorflow.python.framework.errors_impl.InvalidArgumentError: image_size must contain 3 elements[4]

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):打开\显示\保存图像

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值