开发者

Python (creating a negative of this black and white image)

开发者 https://www.devze.com 2023-01-22 07:35 出处:网络
I am trying to create a negative of this black and white image. The opposite of white (255) is black (0) and vice versa. The opposite of a pixel with a value of 100 is 155.

I am trying to create a negative of this black and white image. The opposite of white (255) is black (0) and vice versa. The opposite of a pixel with a value of 100 is 155. I cannot use convert, invert, point, eval, lambda.

Here is my code but it doesnt work yet. Could you please let me know which part i am wrong.

def bw_negative(filename):
    """
    This function creates a black and white negative of a bitmap image
    using the following parameters:
       filename is the name of the bit开发者_如何学JAVAmap image
    """

    #Create the handle and then create a list of pixels.
    image = Image.open(filename)
    pixels = list(image.getdata())

    pixel[255] = 0
    pixel[0] = 255

    for i in range(255,0):
        for j in range(0,255):
            pixel[i] = j
            print pixels[i]


    image.putdata(pixels)
    image.save ('new.bmp')


Python is an interpreted language, which has the advantage that you can use an interactive interpreter-session to try out things. Try to open the image file in an interactive session and look at the list you get from list(image.getdata()). Once you understand what that list contains, you can think about a way to invert the image.

0

精彩评论

暂无评论...
验证码 换一张
取 消