jeudi 2 avril 2020

turning picture into black and white using if statement to determine pixel

def black_and_white(img: Image) -> Image:

    img_width, img_height = img.size
    pixels = img.load()  # create the pixel map

    # for every pixel
    for i in range(img_width):
        for j in range(img_height):
            r, g, b = pixels[i, j]
            if r < 255/2:
                r = 0
            else:
                r = 255
            if g < 255/2:
                g = 0
            else:
                g = 255
            if b < 255/2:
                b = 0
            else:
                b = 255
            pixels[i, j] = (r, g, b)

    return img

using pillow I tried to turn a color image into black and white image, using if statement to determine that if each pixel is closer to white or black and in turnm turn them into accordingly. but the result i got was a slightly highlted picture instead of a complete black and white picture, i was wondering what step did i do it wrong?

Aucun commentaire:

Enregistrer un commentaire