Last active
March 14, 2023 16:48
-
-
Save pcolladosoto/b51a0add84bd4e550f8d3e71bb589bf4 to your computer and use it in GitHub Desktop.
An image filter based on the Fourier transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
import numpy as np | |
from PIL import Image | |
from scipy import signal | |
def filterLowPass(): | |
return signal.butter(4, 50, 'lp', fs = 1000000, output = 'sos') | |
def main(): | |
img = Image.open(pathlib.Path("Img.jpeg")).convert("RGB") | |
imgArray = np.array(img) | |
imgCpy = imgArray.copy() | |
filter = filterLowPass() | |
filteredRSignal = signal.sosfilt(filter, imgArray[..., 0].flatten()) | |
filteredGSignal = signal.sosfilt(filter, imgArray[..., 1].flatten()) | |
filteredBSignal = signal.sosfilt(filter, imgArray[..., 2].flatten()) | |
imgCpy[..., 0] = filteredRSignal.reshape(imgArray.shape[0], imgArray.shape[1]) | |
imgCpy[..., 1] = filteredGSignal.reshape(imgArray.shape[0], imgArray.shape[1]) | |
imgCpy[..., 2] = filteredBSignal.reshape(imgArray.shape[0], imgArray.shape[1]) | |
Image.fromarray(imgArray[..., 1].flatten()).save(pathlib.Path("ImgGreen.jpeg")) | |
Image.fromarray(imgCpy).save(pathlib.Path("ImgFiltered.jpeg")) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment