Convert RGB to black OR white

后端 未结 8 1058
我在风中等你
我在风中等你 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)
    
    0 讨论(0)
  • Pillow, with dithering

    Using pillow you can convert it directly to black and white. It will look like it has shades of grey but your brain is tricking you! (Black and white near each other look like grey)

    from PIL import Image 
    image_file = Image.open("cat-tied-icon.png") # open colour image
    image_file = image_file.convert('1') # convert image to black and white
    image_file.save('/tmp/result.png')
    

    Original:

    meow meow color cat

    Converted:

    meow meow black and white cat

    0 讨论(0)
提交回复
热议问题