在python中反转图像颜色



我正试图反转图像的颜色,我目前正试图找出如何编写函数来实现。

IMAGE_URL = "https://codehs.com/uploads/c709d869e62686611c1ac849367b3245"
IMAGE_WIDTH = 280
IMAGE_HEIGHT = 200
image = Image(IMAGE_URL)
image.set_position(70, 70)
image.set_size(IMAGE_WIDTH, IMAGE_HEIGHT)
add(image)

# Write this function!
def invert_pixel(pixel):
pass
# Applies invert filter to picture
def change_picture():
for x in range(image.get_width()):
for y in range(image.get_height()):
pixel = image.get_pixel(x,y)
new_colors = invert_pixel(pixel)
image.set_red(x, y, new_colors[0])
image.set_green(x, y, new_colors[1])
image.set_blue(x, y, new_colors[2])

# Give the image time to load
print("Inverting Image ....")
print("Might take a minute....")
timer.set_timeout(change_picture, 1000)```

如果你使用的是1色阶,方法如下:

def invert_pixel(pixel):
return tuple(1 - c for c in pixel)

如果您使用的是255量表:

def invert_pixel(pixel):
return tuple(255 - c for c in pixel)