I need to extract the (x, y)
coordinates from a given image file using Python.
I want the data to be in the following format
pixel# x y
1 0 0
2 1 0
.
.
301 0 1
302 1 1
.
.
XX,000 299 199
for any size of image file I have.
Currently I am using this script:
from PIL import Image
import numpy as np
import sys
# load the original image
img_file = Image.open("inputimage_005004.jpg")
img_file.show()
# get original image parameters...
width, height = img_file.size
format = img_file.format
mode = img_file.mode
# Make image Greyscale
img_grey = img_file.convert('L')
img_grey.save('result.jpg')
img_grey.show()
value = np.asarray(img_grey.getdata(),dtype=img_grey.float64).reshape((img_grey.size[1],img_grey.size[0]))
np.savetxt("outputdata.csv", value, delimiter=',')
However I am getting an error at the 2nd last line where
value = np.asarray(...)
The error is:
AttributeError: 'Image' object has no attribute 'float64`
Also, I would like the output to be similar to the one in this StackOverflow question:
Extract x,y coordinates of each pixel from an image in Python .
How can I combine my current script with the above link and fix my present error?
EDIT:
The error is fixed, but I am not getting the (x, y)
coordinates of the image file correctly. It gives me a very long number like this:
2.270000000000000000e+02,2.370000000000000000e+02,2.270000000000000000e+02,2.320000000000000000e+02,2.330000000000000000e+02,...
But, I would like it to be in the format as shown above. How can I achieve this?