Home > Back-end >  TypeError: split() takes 1 positional argument but 2 were given
TypeError: split() takes 1 positional argument but 2 were given

Time:08-25

I have looked at the questions similar, but they are all for user defined functions. Mine is using the default .split() function.

The answers for the other questions indicate that I am missing a "self" argument, but I don't see how I can implement for this case.

My code:

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)

The error I get:

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

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

Any suggestions?

CodePudding user response:

M2 is a PIL.Image object. The PIL.Image.split() method takes no additional arguments (the implied argument being self, the Image in question.

You're thinking of str.split(), which takes in a separator and a maximum number of splits (if you want to specify a maximum).

Perhaps you meant to split M1?

  • Related