Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading