我正在尝试编写一个函数,该函数将像素作为参数并反转像素中的每种颜色,然后将新颜色值作为元组返回。将不胜感激这是我当前的代码:
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 your function here. Loop through each pixel
# and set each pixel to have a zero blue value.
#################################################
def remove_blue():
pass
# Give the image time to load
print("Removing Blue Channel ....")
print("Might take a minute....")
timer.set_timeout(remove_blue, 1000)
答案 0 :(得分:-1)
from PIL import Image
# open image
image = Image.open('pic.jpeg')
# show the image (optional)
image.show()
# load the image into memory
image_data = image.load()
# obtain sizes
height,width = image.size
# loop over and change blue value to 0
for loop1 in range(height):
for loop2 in range(width):
r,g,b = image_data[loop1,loop2]
image_data[loop1,loop2] = r,g,0
image.save('changed.jpeg')
对应的函数是
def remove_blue(image):
image = Image.open('pic.jpeg')
# show the image (optional)
image.show()
# load the image into memory
image_data = image.load()
# obtain sizes
height,width = image.size
# loop over and change blue value to 0
for loop1 in range(height):
for loop2 in range(width):
r,g,b = image_data[loop1,loop2]
image_data[loop1,loop2] = r,g,0
# return image
image.save('changed.jpeg')