I have one dataset containing images X of type ( numpy array) and one target csv file as Y which has counts of cells (type : pandas dataframe, that I have converted to numpy array), both are now read as numpy arrays. Essentially, I am creating a training dataset to train my images with target dataset using cnn model.

For the code below i am getting an error that argument is bad.

Any help on how to solve this?

                    **image = cv2.imread(img_path)**

Full code:

def load_data():
    import os

    datasets =  (X,Y)
    images = []
    labels = []

    # iterate through training and test sets
    count =0
    for dataset in datasets:

        # iterate through folders in each dataset
        for folder in dataset:

                    if folder in ['plasma']: label = 'T4'
                    elif folder in ['lymphocyte']: label = 'T3'
                    elif folder in ['epithelial']: label = 'T2'
                    elif folder in ['neutrophil']: label = 'T1'
                    elif folder in ['eosinophil']: label = 'T5'
                    elif folder in ['connective']: label = 'T6'

                    img_path = np.append(dataset, folder)
                    **image = cv2.imread(img_path)**
                    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                # resize the image
                    image = cv2.resize(image, image_size)

                # Append the image and its corresponding label to the output


                    images = np.array(images, dtype = 'float32')
                    labels = np.array(labels, dtype = 'int32')

    return images, labels


TypeError                                 Traceback (most recent call last)
<ipython-input-145-a8d2a3fd920f> in <module>
----> 1 images, labels = load_data()

<ipython-input-144-1acddd84d1e7> in load_data()
     25                     img_path = np.append(dataset, folder)
---> 26                     image = cv2.imread(img_path)
     27                     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

TypeError: bad argument type for built-in operation


1 Answer 1


By defintion cv2.imread() expect the path: A string representing the path of the image to be read. The image should be in the working directory or a full path of image should be given

Unclear on your datasets structure and what are you keeping inside.

Still at this line img_path = np.append(dataset, folder)
img_path is an array ( np.append will return an array ) and cannot be passed to the imread() function which is clear from the error (

bad argument type for built-in operation


  • $\begingroup$ Thanks for your answer. Is there any other way to read the two (datasets and folder )together, without using the paths or directory? $\endgroup$
    – Ann09
    Commented Mar 24, 2022 at 2:39
  • $\begingroup$ Can you update the question with datasets structure and example $\endgroup$ Commented Mar 24, 2022 at 2:45
  • $\begingroup$ I have updated the question. $\endgroup$
    – Ann09
    Commented Mar 24, 2022 at 2:56
  • $\begingroup$ Still not clear on the Dataset structure ( share 1, or 2 rows from X and Y or a reproducable code so that we can fix the issues.) . Assuming you are trying to construct the path using dataset and folder , try this image_path = os.path.join(dataset, folder). $\endgroup$ Commented Mar 24, 2022 at 3:09
  • $\begingroup$ Here, X is a matrix containing N 256x256x3 images of size 256x256 pixels and Y is an Nx6 matrix with each row corresponding to a single image patch and each column corresponding to the 6 types of cells. With os.path.join, getting this error: TypeError: expected str, bytes or os.PathLike object, not numpy.ndarray $\endgroup$
    – Ann09
    Commented Mar 24, 2022 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.