1

我是 Python 的新手,我正在尝试学习用 Python 处理图片。下面这段代码是创建图片的镜像,然后将其淡化为白色。但是当我执行代码时出现以下错误“错误是:'int'和'function'不适当的参数类型。尝试使用无效类型的参数调用函数。这意味着你做了诸如试图将字符串传递给需要整数的方法。” 我不确定是什么导致了这个错误,我需要帮助。

def blendWhite(pixcel, fadeAmount):
    newRed = 255 * fadeAmount + getRed(pixel) * (1 - fadeAmount)
    newGreen = 255 * fadeAmount + getGreen(pixel) * (1 - fadeAmount)
    newBlue = 255 * fadeAmount + getBlue(pixel) * (1 - fadeAmount)
    setColor(pixel, makeColor(newRed, newGreen, newBlue))

def copyAndMirrorCat():
    catFile = getMediaPath("caterpillarSmall.jpg")
    catPict = makePicture(catFile)
    width = getWidth(catPict)
    height = getHeight(catPict)
    canvas = makeEmptyPicture(width, height * 2)
    # Now, do the actual copying
    for x in range(0, width):
        for y in range(0, height):
            color = getColor(getPixel(catPict, x, y))
            setColor(getPixel(canvas, x, y), color)
            h = height * 2
            fadeAmount(y, h)
            blendWhite(getPixel(canvas, x, (height * 2) - y - 1), fadeAmount)
    show(catPict)
    show(canvas)
    return canvas

def fadeAmount(y, h):
    fm = (h - y) / float(h) + 0.15
    if fm > 1:
        fm = 1
    return fm  

结果如下所示:http: //imageshack.us/a/img442/326/mirrorlinearwithwhite.jpg

4

1 回答 1

1

如果您查看您所说的行:

fadeAmount(y,h) 
blendWhite(getPixel(canvas,x,(height*2)-y-1),fadeAmount)

您会看到您调用时fadeAmount()没有将值存储在变量中。然后,您尝试将函数传递给blendWhite(). 这只是行不通。您需要存储该值并传递它,例如:

fa = fadeAmount(y,h)
blendWhite(getPixel(canvas,x,(height*2)-y-1),fa)

...或者只是将其全部声明为:

blendWhite(getPixel(canvas,x,(height*2)-y-1),fadeAmount(y,h))
于 2012-10-04T22:47:01.223 回答