Python(创建这张黑白图像的负片)

-1 投票
1 回答
2686 浏览
提问于 2025-04-16 06:02

我想把这张黑白图片做成负片。白色的值是255,黑色的值是0,它们是相反的。一个像素值为100的颜色,它的相反色就是155。

我不能使用convert、invert、point、eval和lambda这些方法。

这是我的代码,但还没有成功。你能告诉我哪里出错了吗?

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 bitmap 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')

1 个回答

1

Python是一种解释型语言,这意味着你可以使用一个交互式解释器来尝试各种操作。这就像一个实验室,你可以随意测试。你可以在这个交互式会话中打开一个图片文件,然后查看通过list(image.getdata())得到的列表。一旦你明白了这个列表里有什么内容,你就可以考虑如何将图片反转过来了。

撰写回答