PIL无法处理此数据类型

Cer*_*rin 7 python numpy python-imaging-library

我试图在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接受的数据类型?

unu*_*tbu 8

我想也许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')
Run Code Online (Sandbox Code Playgroud)