开发者

fft and array-to-image / image-to-array-conversion

I want to make a fourier-transformation of an image. But how can I change the picture to an array? And after this I think I should use numpy.fft.rfft2 for the transformation. And how to change back from the array to the image? Thanks in ad开发者_开发知识库vance.


You can use the PIL library to load/save images and convert to/from numpy arrays.

import Image, numpy
i = Image.open('img.png')
i = i.convert('L')    #convert to grayscale
a = numpy.asarray(i) # a is readonly

b = abs(numpy.fft.rfft2(a))

j = Image.fromarray(b)
j.save('img2.png')

I used abs above because the result of the FFT has complex values so it doesn't really make sense to convert it directly to an image. The conversion to grayscale is done so that the FFT is done on a single channel only - you can choose another way to pick a channel instead, or pass the correct axes parameter to rfft2 and later extract the channel you need.

Edit:

To also perform an inverse FFT and get back the original image, the following works for me:

import Image, numpy
i = Image.open('img.png')
i = i.convert('L')    #convert to grayscale
a = numpy.asarray(i)

b = numpy.fft.rfft2(a)
c = numpy.fft.irfft2(b)

j = Image.fromarray(c.astype(numpy.uint8))
j.save('img2.png')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜