Grayscale image for NumPy array for Fourier transform

I am currently using PIL and NumPy. I have a color image pngand I want:

  • Read it in shades of gray
  • Convert to NumPy Array
  • FFT execution in an array
  • Display image

This is what I am trying (in IPython w / --pylab):

In [1]: import Image

In [2]: img = Image.open('ping.png').convert('LA')

In [3]: img_as_np = np.asarray(img)

In [4]: img_as_np
Out[4]: array(<Image.Image image mode=LA size=1000x1000 at 0x105802950>, dtype=object)

In [5]: img_fft = fft.fft2(img_as_np) // IndexError: index out of range for array
+5
source share
3 answers

"L" "LA" convert(). "LA" -, numpy.asarray , . -, numpy. "L".

+4

, PIL 1.1.6, , numpy , Image. , img_as_np , Image ( Out[4]).

- np.asarray(img.getdata()), num_pixels x num_channels 0 255 ( , png, ).

img_as_np = np.asarray(img.getdata()).reshape(img.size[1], img.size[0], -1)

(). 255, 0 1, (, matplotlib imshow).

+4

Using this for an image:

>>> from PIL import Image
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> Image.__version__
'1.1.7'
>>> img = Image.open('lena.png').convert('L')
>>> data = np.asarray(img.getdata()).reshape(img.size)
>>> fft = np.fft.fft2(data)
>>> fft[0, 0] = 0 # remove DC component for visualization
>>> plt.imshow(np.abs(np.fft.fftshift(fft)), interpolation='nearest')
<matplotlib.image.AxesImage object at 0x046012F0>
>>> plt.show()
>>> plt.imshow(np.abs(np.fft.fftshift(fft))[224:288, 224:288], interpolation='nearest')
<matplotlib.image.AxesImage object at 0x0476ED70>
>>> plt.show()

enter image description here

+1
source

All Articles