[Python] How do I create a 2-dimensional numpy array object with zero size?

Discussão em 'Python' iniciado por Stack, Maio 5, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a 2-dimensional numpy array object to store image data.

    The way the image data will be constructed is by continually appending blocks of 2-dimensional data to this array.

    I tried this sequence of steps:

    1. Create an empty numpy.array with the correct data type for greyscale images using image = numpy.array([], dtype=numpy.uint8)
    2. I tried to use reshape to convert it into something 2-dimensional, since supplying a list of lists [[]] to numpy.array did not seem to produce a 2-dimensional object. image = numpy.reshape(image, shape=(28,28)) (Each image will be 28x28 pixels.)
    3. However, when trying to use numpy.concatenate to append smaller blocks of images to the output image, I encountered an error. image = numpy.concatenate(image, sub_image)

    TypeError: only integer scalar arrays can be converted to a scalar index


    I am aware that using concatenate will repeatedly copy blocks of memory, and therefore this is not an efficient solution.

    After fixing the code:

    numpy.concatenate((image, sub_image), axis=0)


    ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 28

    Continue reading...

Compartilhe esta Página