繁体   English   中英

TypeError: split() 接受 1 个位置参数,但给出了 2 个

[英]TypeError: split() takes 1 positional argument but 2 were given

我看过类似的问题,但它们都是针对用户定义的功能。 我的使用的是 default.split() function。

其他问题的答案表明我缺少“自我”论点,但我不知道如何为这种情况实施。

我的代码:

import os
import numpy as np
from PIL import Image
from numpy import asarray

def filterBlankImages(maskInputPath, maskOutputPath, imgType = '.jpg', imgInputPath = "Same", imgOutputPath = "Same"):
    if imgInputPath == "Same":
        imgInputPath = maskInputPath
    if imgOutputPath == "Same":
            imgOutputPath = maskOutputPath    
    for M1 in os.listdir(maskInputPath):
        if M1.endswith(".png"):
            M2 = Image.open(maskInputPath + M1)
            data = asarray(M2)
            data_sum = np.sum(data)
            if data_sum == 0:
                I1 = M2.split("_")[0]
                I2 = M2.split("_")[1].split("_cp")[0]
                I3 = I1 + '_' + I2 + imgType
                os.rename(imgInputPath + I3, imgOutputPath + I3)
                os.rename(maskInputPath + M1, maskOutputPath + M1)

我得到的错误:

File "/Users/msshah/Downloads/PreprocessingFunctions.py", line 74, in filterBlankImages
    I1 = M2.split("_")[0]

TypeError: split() takes 1 positional argument but 2 were given

有什么建议么?

M2PIL.Image object。 PIL.Image.split()方法不需要额外的 arguments (隐含的参数是self ,有问题的Image

您正在考虑str.split() ,它接受分隔符和最大拆分数(如果您想指定最大值)。

也许您打算拆分M1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM