LoginSignup
0
0

More than 1 year has passed since last update.

pillow 3次元配列のグレースケール画像削除

Posted at

グレースケールの画像を削除しようとして、2次元配列のものだけ削除すればいいかと思い、 shapeが2のもの削除してもグレースケールの画像は、削除されない場合がある。
なぜなら、3次元(RGB)のグレースケール画像があるから。(隠れグレースケール)
しかし、グレースケール画像の配列の中身は、同じなので、同じものを削除すれば大丈夫。

※ np.all(R == G == B) はエラーが出る。

def delete_gray(root):
    path_list = glob.glob(root+'*')
    for i in path_list:
        image = Image.open(i)
        image = np.array(image)
        R = image[:,:,0]
        G = image[:,:,1]
        B = image[:,:,2]
        if (np.all(R==G)) and (np.all(G==B)):
            os.remove(i)
            print('Delete {}'.format(i))

if __name__ == '__main__':
    delete_gray(root='/Users/name/DATASETS/path/')
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0