I'm working with Python and Numpy to take several images of the same pixel dimension and create an 2D array, so each row of the array represents one image and each column will represent the pixel at a certain location.
To achieve this, I have read in the image files and tried to use numpy.concatenate. The code is
#url of picture data
X_p = data.link
#list for storing the picture data
X= []
#read in the image from the url, and skip poster with 404 error
for url in X_p:
try:
loadimg = urllib.request.urlopen(url)
image_file = io.BytesIO(loadimg.read())
img = Image.open(image_file)
#Concatenate to linearize
X.append(np.concatenate(np.array(img)))
#404 error
except urllib.error.HTTPError as err:
if err.code == 404:
continue
else:
raise
#cast the list into numpy array
X = np.array(X)
#test to see if X is in correct dimension
print(X.shape)
I ran this code and the shape of X comes out in this format every single time
(number of images, height X width, 3)
for instance, if I load 12 image urls of 200x200 pixels, the outcome is
(12, 40000, 3)
What I need is to get rid of the 3 at the end, and it's difficult when I do not even understand where the 3 comes from.
I assume the problem I have is appending or concatenating at the wrong place. when I removed the np.concatenate, it simply did showed (12, 200, 200, 3).
I've searched online for numpy image processing and concatenations but I did not run across anything that would explain and fix what's happening.
Any and all help is appreciated. Thank you in advance for spending the time to read this post and answering..