Skip to main content
 首页 » 编程设计

python之在我的代码中遇到错误,但我无法确定原因。 (getpixel() 接受 2 个位置参数,但给出了 3 个)

2024年12月31日898°冷暖

我正在为一个项目编程,这样我就可以很容易地确定我想为网站的图像填充多少像素。然而,我遇到了这个错误:getpixel() takes 2 positional arguments but 3 were given. 我的代码如下:

from PIL import Image 
import PIL.ImageOps 
 
 
background = Image.open("background.png") 
target = input("Please enter the name of your image (including file type)") 
targetImage = Image.open(target) 
area = targetImage.size 
targetColouredCoords = [] 
 
 
newArea = (area[0] + 1, area[1] + 1) 
borderedTarget = PIL.ImageOps.expand(targetImage, border=10, fill="black") 
targetValues = list(borderedTarget.getdata()) 
 
 
def analyser(): 
    pixelOne = 1 
    pixelTwo = 1 
    completedSearch = 0 
    currentPixel = borderedTarget.getpixel(pixelOne, pixelTwo) 
 
    def colourChecker(): 
        if currentPixel != (0, 0, 0): 
            targetColouredCoords.append((pixelOne, pixelTwo)) 
 
    while completedSearch != len(targetValues): 
        while pixelOne != newArea[0]: 
            while pixelTwo != newArea[1]: 
                colourChecker() 
                pixelTwo += 1 
                completedSearch += 1 
            pixelTwo = 1 
            pixelOne += 1 
 
 
analyser() 

我在整个代码中唯一提供 3 个参数的行是 18,但我不明白这行代码是如何不正确的,或者代码被突出显示为问题(第 26 行)是如何错误的。在删除错误之前我无法继续编码,因此非常感谢您的帮助。

请您参考如下方法:

再看一遍,输入必须是一个元组:

getpixel( (pixelOne,pixelTwo) )

Source