Convert RGB to black OR white

后端 未结 8 1145
我在风中等你
我在风中等你 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:40

    And you can use colorsys (in the standard library) to convert rgb to hls and use the lightness value to determine black/white:

    import colorsys
    # convert rgb values from 0-255 to %
    r = 120/255.0
    g = 29/255.0
    b = 200/255.0
    h, l, s = colorsys.rgb_to_hls(r, g, b)
    if l >= .5:
        # color is lighter
        result_rgb = (255, 255, 255)
    elif l < .5:
        # color is darker
        result_rgb = (0,0,0)
    

提交回复
热议问题