I am experiencing some bugs with this code that is supposed to write the info from an image into a csv file.
Currently I have this piece of code that writes all the pixels of the image [its X,Y coordinates to be exact] to a csv file.
import PIL
import numpy as np
img_grey = Image.open('walkingpath_005004.jpg').convert('L')
print(img_grey.size)
# (300, 241)
x = 3
y = 4
#pix = img_grey.load()
#print(pix[x,y])
# Taken from: https://stackoverflow.com/a/60783743/11089932
xy_coords = np.flip(np.column_stack(np.where(np.array(img_grey) >= 0)), axis=1)
# Add pixel numbers in front
pixel_numbers = np.expand_dims(np.arange(1, xy_coords.shape[0] + 1), axis=1)
#get rgb data
pixels = list(img_grey.getdata())
width, height = img_grey.size
pixels = np.asarray(img_grey)
value = np.hstack([pixel_numbers, xy_coords, pixels])
print(value)
# [[ 1 0 0]
# [ 2 1 0]
# [ 3 2 0]
# ...
# [72298 297 240]
# [72299 298 240]
# [72300 299 240]]
# Properly save as CSV
np.savetxt("outputdata.csv", value, delimiter='\t', fmt='%4d')
I need the RGB data of each pixel in 3 extra columns. As such, the format should be
PixelNumber X Y R G B
Currently faced with this bug:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 258063 and the array at index 2 has size 509
I was looking at this link How to read the RGB value of a given pixel in Python?
but not sure how I can go about with incorporating it into my existing code.
Any and all help is appreciated!
Thank you!