pytorch+numpy+PIL将dat文件读取为图像

Posted 小明250

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytorch+numpy+PIL将dat文件读取为图像相关的知识,希望对你有一定的参考价值。

import os
import torch
import numpy as np
from PIL import Image
path = "D:/OAdata/Camera_3"
datanames = os.listdir(path)
fullpath = path + '/' + datanames[100]
print(fullpath)
data1 = np.fromfile(fullpath, dtype=np.uint8)
x = torch.from_numpy(data1)
d2 = x.reshape(1024, 1280)
print(d2)
d2 = d2.detach().numpy()
img = Image.fromarray(d2)
img.show()

以上是关于pytorch+numpy+PIL将dat文件读取为图像的主要内容,如果未能解决你的问题,请参考以下文章

为啥 PIL 经常与 Pytorch 一起使用?

PyTorch - 获取 'TypeError: pic 应该是 PIL Image 或 ndarray。得到 <class 'numpy.ndarray'>' 错误

torch 中各种图像格式转换

PIL中的Image和numpy中的数组array相互转换

如何将 .tif PIL 图像转换为火炬张量?

pytorch图像基本操作