如何用Python将图像转换为二维数组并存储为文本文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计326个文字,预计阅读时间需要2分钟。
原文链接:[原文链接]使用Python将图片输出为二维数组,并保存到txt文件中。
pythonimport numpy as npfrom PIL import Image
读取图片image_path='path_to_your_image.jpg'image=Image.open(image_path)
将图片转换为灰度图gray_image=image.convert('L')
将图片转换为二维数组image_array=np.array(gray_image)
将二维数组保存到txt文件中with open('image_array.txt', 'w') as file: for row in image_array: file.write(' '.join(map(str, row)) + '\n')
原文链接
使用Python将图片输出为二维数组,并保存到txt文件中。
代码如下:
# coding=utf8 from PIL import Image import numpy as np from scipy import misc import matplotlib.pyplot as pyplot #读图片 def loadImage(): im = Image.open("0001.jpg") #读取图片 im.show() #显示原图 im = im.convert("L") #转换成灰度图 data = im.getdata() data = np.matrix(data) #Image类返回矩阵的操作 data = np.reshape(data,(304,720)) #变换成304*720 new_im = Image.fromarray(data) #调用Image库,数组归一化 new_im.show() #显示新图片 misc.imsave('new_img.jpg', new_im) #保存新图片到本地 return data #写数据 def Writedata(data): filename = 'C:\\Users\\DZF\\Desktop\\negative.txt' #数据文件保存位置 row = np.array(data).shape[0] #获取行数n with open(filename,'w') as f: # 若filename不存在会自动创建,写之前会清空文件 for i in range(0,row): f.write(str(data[i][0:])) f.write("\n") data = loadImage() Writedata(data)学习更多编程知识,请关注我的公众号:
代码的路
本文共计326个文字,预计阅读时间需要2分钟。
原文链接:[原文链接]使用Python将图片输出为二维数组,并保存到txt文件中。
pythonimport numpy as npfrom PIL import Image
读取图片image_path='path_to_your_image.jpg'image=Image.open(image_path)
将图片转换为灰度图gray_image=image.convert('L')
将图片转换为二维数组image_array=np.array(gray_image)
将二维数组保存到txt文件中with open('image_array.txt', 'w') as file: for row in image_array: file.write(' '.join(map(str, row)) + '\n')
原文链接
使用Python将图片输出为二维数组,并保存到txt文件中。
代码如下:
# coding=utf8 from PIL import Image import numpy as np from scipy import misc import matplotlib.pyplot as pyplot #读图片 def loadImage(): im = Image.open("0001.jpg") #读取图片 im.show() #显示原图 im = im.convert("L") #转换成灰度图 data = im.getdata() data = np.matrix(data) #Image类返回矩阵的操作 data = np.reshape(data,(304,720)) #变换成304*720 new_im = Image.fromarray(data) #调用Image库,数组归一化 new_im.show() #显示新图片 misc.imsave('new_img.jpg', new_im) #保存新图片到本地 return data #写数据 def Writedata(data): filename = 'C:\\Users\\DZF\\Desktop\\negative.txt' #数据文件保存位置 row = np.array(data).shape[0] #获取行数n with open(filename,'w') as f: # 若filename不存在会自动创建,写之前会清空文件 for i in range(0,row): f.write(str(data[i][0:])) f.write("\n") data = loadImage() Writedata(data)学习更多编程知识,请关注我的公众号:
代码的路

