当前位置:网站首页>numpy数组和图片互转
numpy数组和图片互转
2022-07-27 05:23:00 【sin_404】
一、numpy array的存储和加载:
- load() 和 save() 函数是读写文件数组数据的两个主要函数,默认情况下,数组是以未压缩的原始二进制格式保存在扩展名为 .npy 的文件中。
- savez() 函数用于将多个数组写入文件,默认情况下,数组是以未压缩的原始二进制格式保存在扩展名为 .npz 的文件中。
- loadtxt() 和 savetxt() 函数处理正常的文本文件(.txt 等)
二、narray数组还原图片
import numpy as np
from PIL import Image
x_train = np.load('x_train.npy')
y_train = np.load('y_train.npy')
# np.array将数据转化为数组 np.reshape将一维数组reshape成(28*28) dtype转换为int8数据类型
# im_data = npr.array(np.reshape(28,28), dtype=np.int8) # 如果nparray是一维的这里需要换员成二维图
# # 将数组还原成图片 Image.fromarray方法 传入数组 和 通道
img = Image.fromarray(x_train[0])
# img.save('1.jpg')
img.show() # 显示图片
# 拿对应的标签
arr_data = y_train[0]
print(arr_data) # one-hot形式三、numpy数组和图片互转方式
#file转numpy,这里可以读取图片二进制字符串
npimg = np.fromstring(file.read(), np.uint8)
# 使用opencv的imdecode,把np转img
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
# 使用pil的image.fromarray
img=Image.fromarray(x_train[0])
边栏推荐
- Bug classification and defect and CSV file testing
- Database commands
- Source code compilation and installation LNMP and discuz Forum
- 装饰器函数与类装饰器的使用
- Shell脚本备份MySQL数据库
- Learning records of programming -- Lesson 2 [first knowledge of C language]
- Converting ArcGIS style stylesheet files to GeoServer SLD files
- Shell programming specifications and variables
- Shell脚本编写格式
- 测试基础概括
猜你喜欢
随机推荐
Source code compilation and installation lamp and discuz Forum
Summary of test basis
KVM command set management virtual machine
Basic knowledge of English: Rules for using attributives Part 2
Basic file operation of cmder
Installation, configuration and use of gradle
DNS故障分析优化
Shell programming specification and redirection and pipeline operation
Shell programming specifications and variables
关于testng相关标签的使用
[first blog - outlook]
正则表达式
Interpretation of unity desktop version 7.6
基于Apache下ab工具进行网站压力性能测试
DNS域名解析服务
Constraints and design of database
shell函数
Binary tree - search tree
Basic knowledge of English: juxtaposition structure
DHCP principle and configuration









