Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
dynamicslab
GitHub Repository: dynamicslab/databook_python
Path: blob/master/CH02/CH02_SEC06_2_Compress.ipynb
597 views
Kernel: Python 3
from matplotlib.image import imread import numpy as np import matplotlib.pyplot as plt import os plt.rcParams['figure.figsize'] = [12, 8] plt.rcParams.update({'font.size': 18}) A = imread(os.path.join('..','DATA','dog.jpg')) B = np.mean(A, -1); # Convert RGB to grayscale Bt = np.fft.fft2(B) Btsort = np.sort(np.abs(Bt.reshape(-1))) # sort by magnitude # Zero out all small coefficients and inverse transform for keep in (0.1, 0.05, 0.01, 0.002): thresh = Btsort[int(np.floor((1-keep)*len(Btsort)))] ind = np.abs(Bt)>thresh # Find small indices Atlow = Bt * ind # Threshold small indices Alow = np.fft.ifft2(Atlow).real # Compressed image plt.figure() plt.imshow(Alow,cmap='gray') plt.axis('off') plt.title('Compressed image: keep = ' + str(keep))
Image in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebook