Convert an image to 2D array in python

前端 未结 3 836
独厮守ぢ
独厮守ぢ 2021-02-07 16:24

I want to convert an image to 2D array with 5 columns where each row is of the form [r, g, b, x, y]. x, y is the position of the pixel and r,g,b are the pixel value

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 16:46

    I had to write this recently and ended up with

    indices = np.dstack(np.indices(im.shape[:2]))
    data = np.concatenate((im, indices), axis=-1)
    

    Where im is a numpy array. You are probably better off reading the images straight into numpy arrays with

    from scipy.misc import imread
    im = imread("farm.jpg")
    

    Or, better still if you have Scikit Image installed

    from skimage.io import imread
    im = imread("farm.jpg")
    

提交回复
热议问题