Issue
So I have a np array of np arrays; each np array within it represents an image and all images have the same size. I want to reshape the np array to be just one np array with all the values. This has not been working for me. One of my problems was that when I tried to look at the shape of the np array of np arrays, I would not receive the shape information of np arrays inside.
# Example np array of np arrays. All images in the example should be 2x2x3.
images = np.array(np.array[[[200,200,200],[200,200,200]],[[200,200,200],[200,200,200]]], ...)
# Assuming there are 3 images within, len(images) == 3
print(images.shape) # prints (3,)
print(images[0].shape) # prints (2,2,3)
For this reason, I tried to flatten the array and then reshape it. But there must be some issue with my math or something. When trying to reshape the flatten_images, I am getting a larger number of indexes for the np array than I expected. My math is width of image multiplied by height by color channels by number of images.
flatten_images = np.concatenate(images)
new_image_list = flatten_images.reshape(len(images), 2, 2, 3) #this is where the problem is
The original images array was a bunch of images of different sizes but I resized them all to be the same size. When they were different sizes, it made sense that images.shape would give just the first column information, but now I don't know what to do.
Solution
I figured it out. Some of the image arrays were RGBA format while others were in RGB format. This meant the reshape parameters given were off for those particular images as they had a 4 channel instead of 3.
I went through each image and converting an rgba images to rgb. This allowed the code to work.
Answered By - Kuro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.