读取txt中的像素
数据格式示例: 逗号作为分隔符
代码:
from PIL import Image
import numpy as np
# 读取本地文件,文件格式为txt,将文件中的数据转存在一个list列表中
def readfile(filename):
with open(filename, 'r') as f:
list1 = []
for line in f.readlines():
line_str = line.strip().split(',')#strip()表示删除掉数据中的换行符,split(‘,’)则是数据中遇到‘,’ 就隔开
for element in line_str:
if element != ",":
list1.append(int(element))
return list1
np.set_printoptions(threshold=np.inf) #超过多少为省略号,无限大,不省略
if __name__ == '__main__':
list_result = readfile("txt_feature.txt")
#print( list_result , 3*'\n' )
# 测试的txt中,只有0和1,目标是把1显示为“黑色”,0显示为“白色”;
# 所以将列表中的1替换为0,而0替换为255
for i in range(0, len(list_result)):
'''
if list_result[i] <=127:
list_result[i] = 0
else:
list_result[i] = 255
'''
# 再利用numpy将列表包装为数组
array1 = np.array(list_result)
#array1 = array1[:4096]
print(array1)
# 进一步将array包装成矩阵
data = np.matrix(array1)
# 重新reshape一个矩阵为一个方阵
data = np.reshape(data, (64, -1))
# 调用Image的formarray方法将矩阵数据转换为图像PIL类型的数据
new_map = Image.fromarray(np.uint8(data))
# 显示图像
new_map.show()
'''
if new_map.mode == "P":
new_map = new_map.convert('RGB')
new_map.save('save.jpeg')
'''
参考链接:https://blog.youkuaiyun.com/weixin_41606064/article/details/103412522
原来它读取数字是单个的(0或去),修改后可读取整数
txt文件数据写入
参考:点此
github搜索
点这里
搜索输入举例
rcnn in:readme APs APm language:python