scientific-programming-exer.../ex_45.py

29 lines
701 B
Python
Raw Normal View History

2019-01-23 14:27:28 +00:00
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt
from util.io import readvalue
def positive_int(s):
i = int(s)
if(i <= 0):
raise ValueError("{} is negative".format(i))
return i
file_input_name = readvalue("Input file name > ", str)
file_output_name = readvalue("Output file name > ", str)
cutoff = readvalue("Frequencies to cut > ", positive_int)
fin = misc.imread(file_input_name, flatten=True)
transformed = np.fft.fft2(fin)
new_transformed = np.zeros(transformed.shape, dtype=np.complex)
new_transformed[cutoff:-cutoff, cutoff:-cutoff] = transformed[cutoff:-cutoff, cutoff:-cutoff]
new = np.fft.ifft2(new_transformed).real
misc.imsave(file_output_name, new)