将像素值数组保存到文本fi中

2024-04-19 23:16:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个灰度图像的像素值数组。我想将这些值导出到文本文件或CSV中。我一直在尝试使用各种各样的函数来实现这一点,包括:xlswrite,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()

本质上,我想把“价值”存起来,用在其他地方。在


Tags: csvimageimportformatimgpngmodeshow
1条回答
网友
1楼 · 发布于 2024-04-19 23:16:02

不使用.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

^{pr2}$

注意:x.load()生成一个Pixel访问类的实例,该实例无法使用您提到的csv编写器进行操作。可用于提取和操作像素访问类中单个像素的方法可以在这里找到:http://pillow.readthedocs.io/en/3.1.x/reference/PixelAccess.html

相关问题 更多 >