Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

python - PIL Cannot Handle This Data Type

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')

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

TypeError: Cannot handle this data type

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)).

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...