0

I am trying to parse a folder with contains csv file (These csv files are pixel images position) and store them into a numpy array. When I try to perform this action, I have an error: AttributeError: 'numpy.ndarray' object has no attribute 'append'. I understand that NumPy arrays do not have an append().

However in my code I used the method: images.append(img)

Could you tell what I am doing badly in?

Here my code:

# Create an empty list to store the images
images = []

# Iterate over the CSV files in the img_test folder
for file in os.listdir("img_test"):
    if file.endswith(".txt"):
        # Read the CSV file into a dataframe
        df = pd.read_csv(os.path.join("img_test", file), delim_whitespace=True, header=None, dtype=float)

        # Convert the dataframe to a NumPy array
        image = df.to_numpy()

        # Extract the row and column indices and the values
        rows, cols, values = image[:, 0], image[:, 1], image[:, 2]

        # Convert the row and column indices to integers
        rows = rows.astype(int)
        cols = cols.astype(int)

        # Create a 2D array of the correct shape filled with zeros
        img = np.zeros((1024, 1024))

        # Assign the values to the correct positions in the array
        img[rows, cols] = values

        # Resize the image to 28x28
        img = cv2.resize(img, (28, 28))

        # Reshape the array to a 3D array with a single channel
        img = img.reshape(28, 28, 1)

        # Append the image to the list
        images.append(img)

    # Convert the list of images to a NumPy array
    images = np.concatenate(images, axis=0)

3 Answers 3

1

At the end of the outer for loop you turn images from a list to a NumPy array

images = list()
for file in os.listdir("img_test"):
   if file.endswith(".txt"):
       ...
   images = np.concatenate(images, axis=0) # not a list anymore

You might have missalligned the concatenate and wanted to do it after the end of the for loop.

Sign up to request clarification or add additional context in comments.

Comments

1
# Create an empty list to store the images
images = []

# Iterate over the CSV files in the img_test folder
for file in os.listdir("img_test"):
    if file.endswith(".txt"):
        # Read the CSV file into a dataframe
        df = pd.read_csv(
            os.path.join("img_test", file),
            delim_whitespace=True,
            header=None,
            dtype=float,
        )

        # Convert the dataframe to a NumPy array
        image = df.to_numpy()

        # Extract the row and column indices and the values
        rows, cols, values = image[:, 0], image[:, 1], image[:, 2]

        # Convert the row and column indices to integers
        rows = rows.astype(int)
        cols = cols.astype(int)

        # Create a 2D array of the correct shape filled with zeros
        img = np.zeros((1024, 1024))

        # Assign the values to the correct positions in the array
        img[rows, cols] = values

        # Resize the image to 28x28
        img = cv2.resize(img, (28, 28))

        # Reshape the array to a 3D array with a single channel
        img = img.reshape(28, 28, 1)

        # Append the image to the list
        images.append(img)

    # Convert the list of images to a NumPy array
    cobmined_images = np.concatenate(images, axis=0)

You are initialzing images as list. Then you are creating numpy array with same name. So on the second iteration images is numpy array so it won't have append property.Try using different names as i have done

Comments

0

The indentation of the last line is wrong. You may want to concatenate after the end of the for loop

1 Comment

Thanks, my error was exactly a wrong indentation :-/

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.