PIL无法处理此数据类型
我正在尝试使用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')
但是,当我试图把numpy数组转换回PIL图像时,出现了错误:
TypeError: Cannot handle this data type
不过,a和b这两个数组看起来都是相同的数据类型(uint8),而且使用Image.fromarray(a)
时没有问题。我注意到它们的形状稍微有点不同(a.shape = (1840, 3264, 3) 而 b.shape = (1840, 3264, 2))。
我想知道我该如何修复这个问题,以及PIL支持哪些数据类型?
1 个回答
11
我觉得可能是rfft2
这个操作在错误的轴上进行了。默认情况下,它使用的是最后两个轴:axes=(-2,-1)
。第三个轴代表的是RGB颜色通道。相反,更合理的做法是对空间轴进行FFT,也就是使用axes=(0,1)
:
import Image
import numpy as np
i = Image.open('image.png').convert('RGB')
a = np.asarray(i, np.uint8)
print(a.shape)
b = abs(np.fft.rfft2(a,axes=(0,1)))
b = np.uint8(b)
j = Image.fromarray(b)
j.save('/tmp/img2.png')