Skip to content

Instantly share code, notes, and snippets.

@pcolladosoto
Last active March 14, 2023 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcolladosoto/b51a0add84bd4e550f8d3e71bb589bf4 to your computer and use it in GitHub Desktop.
Save pcolladosoto/b51a0add84bd4e550f8d3e71bb589bf4 to your computer and use it in GitHub Desktop.
An image filter based on the Fourier transform
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