使用 python 为 CodeHS 创建和图像过滤器

问题描述

任务如下: "填写以Image为参数的custom_filter函数,通过某种方式修改图片中的像素,创建新图片,然后返回修改后的Image!

使您的过滤器独一无二。尝试修改图像中每个像素的不同方法。”

这是我到目前为止的代码,它不起作用:

IMAGE_URL = "https://codehs.com/uploads/c709d869e62686611c1ac849367b3245"
IMAGE_WIDTH = 420
IMAGE_HEIGHT = 300
IMAGE_LOAD_TIME = 1000

image = Image(IMAGE_URL)
image.set_size(IMAGE_WIDTH,IMAGE_HEIGHT)
add(image)

"""
 Filter that takes an image as a parameter
 and applies a filter,then returns the filtered image
"""
def custom_filter(image):
    custom_pixel[0] = 255 - pixel[0]
    custom_pixel[1] = 255 - pixel[1]
    custom_pixel[2] = 255 - pixel[2]
    return(custom_pixel[0],custom_pixel[1],custom_pixel[2])


def change_image():
    for x in range(image.get_width()):
        for y in range(image.get_height()):
            pixel = image.get_pixel(x,y)
            new_colors = custom_filter(image)
            image.set_red(x,y,custom_pixel[0])
            image.set_green(x,custom_pixel[1])
            image.set_blue(x,custom_pixel[2])    
    return custom_filter(image)
    

    image = custom_filter(image)
    
    
timer.set_timeout(change_image,IMAGE_LOAD_TIME)

编辑; 这是出现的错误

回溯(最近一次调用):文件 VFS.browser.timer.py 第 13 行,在 f msg='{0.info}\n{0.class.name }: {0.args[0]}'.format(exc) File https://static.codehs.com/gulp/7d014c9ce2d8dbe63839f802b4acce8fd580dd49/abacus/brython-graphics.html?shouldResize=false&canvasWidth=400&canvasHeight=480/__main__ line 25,in change_image for x in range(image.get_width()): UnboundLocalError: local variable 'image ' 在赋值前引用

我不知道代码有什么问题,请帮帮我,我已经想了好几天了,作业昨天到期了。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)