2

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.

4
  • In what way is this unsuccessful? Commented Jan 6, 2017 at 16:02
  • This does not produce errors but I am unsure about how to deal with the data I have generated. Commented Jan 6, 2017 at 16:03
  • do you want convert your image in something like PGM file format? Commented Jan 6, 2017 at 16:58
  • No I just want to save the values from the pixels into a file. Commented Jan 6, 2017 at 17:03

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.