Convert RGB to black OR white

后端 未结 8 1157
我在风中等你
我在风中等你 2020-11-30 23:20

How would I take an RGB image in Python and convert it to black OR white? Not grayscale, I want each pixel to be either fully black (0, 0, 0) or fully white (255, 255, 255).

8条回答
  •  一个人的身影
    2020-11-30 23:41

    I would suggest converting to grayscale, then simply applying a threshold (halfway, or mean or meadian, if you so choose) to it.

    from PIL import Image
    
    col = Image.open('myimage.jpg')
    gry = col.convert('L')
    grarray = np.asarray(gry)
    bw = (grarray > grarray.mean())*255
    imshow(bw)
    

提交回复
热议问题