1.在图像处理的过程中,图像的种类各异,有单通道8bit的,有3通道,24bit的,还有4通道32bit的,当然如果不加判断的读取图片有时候可能会导致代码的bug,所以在读取图片的过程中要先判断图像是什么类型,针对不同的类型图片采取不同的处理方式,如下代码采用PIL来判断图像是几通道图片。

"coding = utf-8"

import shutil
import os,sys
import cv2
from PIL import Image
import numpy as np

input_path = '/data/craft/tc/train/traing_images'
out_path ='/data/craft/tc/train/dan'


filelist = os.listdir(input_path)
for item in filelist:        
    img_path = os.path.join(input_path, item)
    move_path =os.path.join(out_path, item)
    
    
    image = Image.open(img_path)
    image = np.array(image)
    print(item)
    #print('image',image)
    #print('image.size',image.size)
    print('image.shape',image.shape)
    
    
    #2就是单通道图片
    if len(image.shape) == 2:    
        shutil.move(img_path, move_path)
    else:
        continue

更多推荐

python如何判断图像是几通道图片