In [1]:
get_ipython().ast_node_interactivity = 'all' import os import matplotlib.pyplot as plt import numpy as np import matplotlib import math from PIL import Image matplotlib.rcParams['figure.dpi'] = 150 matplotlib.pyplot.rcParams["figure.figsize"] = (10, 10) def imshow(i): plt.imshow(i, cmap="gray") def fft2(i): return np.fft.fftshift(np.fft.fft2(img)) def ifft2(i): i = np.fft.fftshift(i) i = np.fft.ifft2(i) return i def jpegify(img): i = Image.fromarray(np.uint8(img)) i.save("/tmp/test.jpg") return np.array(Image.open("/tmp/test.jpg").convert("L"))
In [2]:
img = Image.open("/home/leo/snowman.jpg") img = img.resize((img.width // 4, img.height // 4)) img = img.convert("L") img = np.array(img) imshow(img)
Out:
In [3]:
fft = fft2(img) imshow(np.log(np.abs(fft)))
Out:
In [4]:
imshow(np.abs(ifft2(fft2(img))))
Out:
In [8]:
fft = fft2(img) center = np.array([img.shape[0] // 2, img.shape[1] // 2]) for y in range(img.shape[0]): for x in range(img.shape[1]): p = np.array([y, x]) dist = np.linalg.norm(center - p) if abs(center[0] - y) > 20 and abs(center[0] - y) < 45 and abs(center[1] - x) > 20 and abs(center[1] - x) < 45: fft[y][x] = 1+0j imshow(np.log(np.abs(fft)))
Out:
In [9]:
img = ifft2(fft) for _ in range(4096): img = jpegify(np.abs(img)) imshow(np.log(np.abs(fft2(img))))
Out:
In [10]:
imshow(img)
Out: