I have an array of pixel values from a greyscale image. I want to export these values into a text file or CSV. I have been trying to to this using various function including: xlsxwrite, write, CSV but I have so far been unsuccessful in doing this. This is what I am working with:
from PIL import Image
import numpy as np
import sys
# load the original image
img_file = Image.open("seismic.png")
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.png')
img_grey.show()
# Save Greyscale values
value = img_grey.load()
Essentially I would like to save 'value' to use elsewhere.
Instead of using the .load() method, you could use this (with 'x' as your grey-scale image):
value = np.asarray(x.getdata(),dtype=np.float64).reshape((x.size[1],x.size[0]))
This is from: Converting an RGB image to grayscale and manipulating the pixel data in python
Once you have done this, it is straightforward to save the array on file using numpy.savetxt
numpy.savetxt("img_pixels.csv", value, delimiter=',')
Note: x.load() yields an instance of a Pixel Access class which cannot be manipulated using the csv writers you mentioned. The methods available for extracting and manipulating individual pixels in a Pixel Access class can be found here: http://pillow.readthedocs.io/en/3.1.x/reference/PixelAccess.html