Home > Enterprise >  How to append images in numpy array?
How to append images in numpy array?

Time:03-09

I'm trying to make a numpy with this shape(number_of_images,32,32) all the images have the same dimensions: 32,32 here is my code:

data=np.array([])
for filename in glob.glob(path '*.tif'): 
    im = np.array([np.array(cv2.imread(filename, cv2.IMREAD_GRAYSCALE))])
    #im = preprocess(im)
    im = NormalizeData(im)
    data=np.append(data,im)

im shape is (1,32,32) however data shape is not the one I wanted it is (113664,)

CodePudding user response:

try using lists and at the end cast to numpy - less confusing

def main():
    cv_img_fake = np.zeros(shape=(1, 32, 32), dtype=np.uint8)
    print('image shape {}'.format(cv_img_fake.shape))

    images = []
    for filename_i in range(10):  # imagine 10 images
        print('filename {}:'.format(filename_i))
        im = cv_img_fake.copy()  # shape 1,32,32
        print('\timage shape {}'.format(im.shape))
        im = im.reshape(-1, im.shape[-1])  # shape 32,32
        print('\timage shape {}'.format(im.shape))

        # do to im whatever you want except changing the dims
        # check after every function by printing dims didn't change - e.g. still 32,32
        # im = NormalizeData(im)
        # print('\timage shape {}'.format(im.shape))
        # im = preprocess(im)
        # print('\timage shape {}'.format(im.shape))
        images.append(im)
    images = np.uint8(images)  # 10 images of 32, 32
    print('images shape {}'.format(images.shape))  # 10,32,32
    return

Output:

image shape (1, 32, 32)
filename 0:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 1:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 2:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 3:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 4:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 5:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 6:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 7:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 8:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 9:
    image shape (1, 32, 32)
    image shape (32, 32)
images shape (10, 32, 32)
  • Related