将像素值数组保存到文本文件中
Saving an array of pixel values into a text file
我有一个灰度图像的像素值数组。我想将这些值导出到文本文件或 CSV 文件中。我一直在尝试使用各种功能来做到这一点,包括:xlsxwrite、write、CSV,但到目前为止我一直没有成功。这就是我正在使用的:
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()
基本上我想保存 'value' 以在其他地方使用。
而不是使用 .load() 方法,你可以使用这个('x' 作为你的灰度图像):
value = np.asarray(x.getdata(),dtype=np.float64).reshape((x.size[1],x.size[0]))
本文来自:Converting an RGB image to grayscale and manipulating the pixel data in python
完成此操作后,可以直接使用 numpy.savetxt
将数组保存到文件中
numpy.savetxt("img_pixels.csv", value, delimiter=',')
注意:x.load() 产生像素访问实例 class,无法使用您提到的 csv 编写器对其进行操作。可在像素访问 class 中提取和操作单个像素的方法可在此处找到:http://pillow.readthedocs.io/en/3.1.x/reference/PixelAccess.html
我有一个灰度图像的像素值数组。我想将这些值导出到文本文件或 CSV 文件中。我一直在尝试使用各种功能来做到这一点,包括:xlsxwrite、write、CSV,但到目前为止我一直没有成功。这就是我正在使用的:
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()
基本上我想保存 'value' 以在其他地方使用。
而不是使用 .load() 方法,你可以使用这个('x' 作为你的灰度图像):
value = np.asarray(x.getdata(),dtype=np.float64).reshape((x.size[1],x.size[0]))
本文来自:Converting an RGB image to grayscale and manipulating the pixel data in python
完成此操作后,可以直接使用 numpy.savetxt
将数组保存到文件中numpy.savetxt("img_pixels.csv", value, delimiter=',')
注意:x.load() 产生像素访问实例 class,无法使用您提到的 csv 编写器对其进行操作。可在像素访问 class 中提取和操作单个像素的方法可在此处找到:http://pillow.readthedocs.io/en/3.1.x/reference/PixelAccess.html