Using python PIL to turn a RGB image into a pure black and white image

后端 未结 5 1986
逝去的感伤
逝去的感伤 2020-12-02 07:06

I\'m using the Python Imaging Library for some very simple image manipulation, however I\'m having trouble converting a greyscale image to a monochrome (black and white) ima

5条回答
  •  Happy的楠姐
    2020-12-02 07:22

    A simple way to do it using python :

    Python
    import numpy as np
    import imageio
    
    image = imageio.imread(r'[image-path]', as_gray=True)
    
    # getting the threshold value
    thresholdValue = np.mean(image)
    
    # getting the dimensions of the image
    xDim, yDim = image.shape
    
    # turn the image into a black and white image
    for i in range(xDim):
        for j in range(yDim):
            if (image[i][j] > thresholdValue):
                image[i][j] = 255
            else:
                image[i][j] = 0
    
    

提交回复
热议问题