我有一个Numpy数组类型的矩阵.我如何将其作为图像写入磁盘?任何格式都有效(png,jpeg,bmp ......).一个重要的限制是PIL不存在.
temp_image 是 (600, 600, 3) 值从 0 到 1。
def pro_process(temp_img, input_size):
img = np.asarray(temp_img).astype('float32')
img = np.array(Image.fromarray(img).resize((input_size, input_size)).convert(3))
return img
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误:
Traceback (most recent call last):
File "S:\Program Files\Python36\lib\site-packages\PIL\Image.py", line 2681, in fromarray
mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 3), '<f4')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "H:\OneDrive\synchronization code\Developing collection\Python\MNet_DeepCDR-master\mnet_deep_cdr_ide\run\Step_3_MNet_test.py", line 56, in <module>
temp_img = pro_process(Disc_flat, CDRSeg_size)
File "S:\Program Files\Python36\lib\site-packages\mnet_deep_cdr\mnet_utils.py", line 18, in pro_process
img = np.array(Image.fromarray(img).resize((input_size, input_size)).convert(3)) …Run Code Online (Sandbox Code Playgroud) 我试图在numpy中使用fft模块:
import Image, numpy
i = Image.open('img.png')
a = numpy.asarray(i, numpy.uint8)
b = abs(numpy.fft.rfft2(a))
b = numpy.uint8(b)
j = Image.fromarray(b)
j.save('img2.png')
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将numpy数组转换回PIL图像时,我收到错误:
TypeError: Cannot handle this data type
Run Code Online (Sandbox Code Playgroud)
但是,a和b数组似乎都具有相同的数据类型(uint8),并且Image.fromarray(a)运行正常.我注意到形状略有不同(a.shape =(1840,3264,3)vs b.shape =(1840,3264,2)).
我确实解决了这个问题并找出了PIL接受的数据类型?
我有一个大小为 (4,3,224,224) 的 Pytorch 张量。当我尝试将第一个张量转换为 Image 对象时,它说:
TypeError: Cannot handle this data type
Run Code Online (Sandbox Code Playgroud)
我运行了以下命令:
img = Image.fromarray(data[0][i].numpy().astype(np.uint8))
Run Code Online (Sandbox Code Playgroud)
其中 data 是 Pytorch 张量
我尝试了其他解决方案,但找不到任何解决方案。
请建议!!
python numpy image-processing python-imaging-library pytorch