本文介绍了PIL无法处理此数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在numpy中使用fft模块:

I'm attempting to use the fft module in numpy:

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图像时,出现错误:

However, when I try and convert the numpy array back to a PIL image, I get the error:

TypeError: Cannot handle this data type

但是,a和b数组似乎都具有相同的数据类型(uint8),并且执行Image.fromarray(a)运行良好.我确实注意到形状略有不同(a.shape =(1840,3264,3)与b.shape =(1840,3264,2)).

However, both a and b arrays appear to have the same data type (uint8), and doing Image.fromarray(a) runs fine. I do notice the shapes are slightly different (a.shape = (1840, 3264, 3) vs b.shape = (1840, 3264, 2)).

我确实解决了这个问题,并找出了PIL接受哪些数据类型?

I do fix this and find out which data types PIL accepts?

推荐答案

我认为rfft2可能是在错误的轴上执行的.默认情况下,它使用最后两个轴:axes=(-2,-1).第三个轴代表RGB通道.取而代之的是,似乎更愿意在空间轴axes=(0,1):

I think perhaps the rfft2 is being performed over the wrong axes.By default, it uses the last two axes: axes=(-2,-1). The third axis represents the RGB channels. Instead, it seems more plausible that one would want to perform an FFT over the spatial axes, 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')

这篇关于PIL无法处理此数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:43
查看更多