edit: copying from my comment to give more context to the problem
One important thing I forgot to mention is my end goal is to add this as a column to a pandas dataframe. I was getting errors when trying to put the pixels array as is into dataframe due to more than one dimension.
I have a 32x32 image that I would like to flatten into a 1-D numpy array representing an object with the RGB values at that pixel.
img = Image.open(img_path)
pixels = np.asarray(img)
width, height = img.size
pixels = pixels.reshape(width * height, 3)
Currently this is the best I can do without losing the grouping of RGB values in one object. With this implementation however I get a 2-D array with each element being an array of the RGB values like this.
shape: (1024, 3)
[[255 255 255]
[255 255 255]
[255 255 255]
...
[255 255 255]
[255 255 255]
[255 255 255]]
I would like my array to have shape (1024,1)
and for each element to be some object (maybe a tuple?) of RGB values. Thanks.
c.f. comment, array as a dataframe or a series:
dataframe = pd.DataFrame(pixels) # As a dataframe with one column per color channel
column = dataframe.apply(tuple, axis=1) # As a Series of (r,g,b) tuples
Then you can use column
as a column of another dataframe, if you so wish
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.