#___________numerical blurring by iterated Laplacian_______________ import numpy as np import matplotlib.pyplot as plt from PIL import Image, ImageFilter,ImageEnhance # Pillow handbook online: # https://pillow.readthedocs.io/en/3.0x/handbook/tutorial.html plt.interactive(True) from skimage import io, color # I had to install scikit-image (skimage) module via #% pip3 install scikit-image from time import time ''' Apply Laplacian operator to an image Try skimage = scikit-image, and PIL = Pillow Unsharp mask the image. ''' def Laplacian_diffusion_step(arr,q,n): # Perform n Laplacian blurring steps. # # Notice that no "newarr" temprary array is declared; # it is not needed, since array arr is modified only after # all calculations on its copies kept by NumPy are done. # In contrast, pixel-wise method needs the explicit temporary # array, otherwise it would overwrite the data in arr needed # for computation of next pixels. This routine processes array # about 80 times (two orders of magnitude) faster than Python # loops in pixel-wise method. # # save borders upp=arr[0,:]; lwr=arr[-1,:]; lft=arr[:,0]; rgt=arr[:,-1] for i in range(n): arr = q*(np.roll(arr,1,axis=1)+np.roll(arr,-1,axis=1)+ \ np.roll(arr,1,axis=0)+np.roll(arr,-1,axis=0))+ \ (1-4*q)*arr # restore borders arr[0,:]=upp; arr[-1,:]=lwr; arr[:,0]=lft; arr[:,-1]=rgt return(arr) def display(arr,title,clean=0): if(clean): plt.cla() plt.figure(figsize=(10.5,8.5)) # or: dpi=120 plt.imshow(pic,cmap='rainbow') plt.title(title) plt.axis('off') plt.show() input(' continue? ') # def norm_and_display(arr,title,nsig): # normalize, truncate N = arr.shape[0]; M = arr.shape[1] a_m = arr.mean() a_s = arr.std() lev_0 = a_m - nsig*a_s lev_1 = a_m + nsig*a_s np.clip(arr,lev_0,lev_1) arr = (arr-lev_0)/(lev_1-lev_0) plt.figure(dpi=120) #(figsize=(6,6)) plt.imshow(arr, cmap='gray') plt.title(title) plt.axis('off') plt.show() #--------Main program-------------------------------------------- # use scikit-image (skimage) # pictures read/written in local directory t0 = time() #pic = io.imread('M81.jpg') pic = pic0c = io.imread('M81.jpg') t1 = time() - t0 # this demonstrates the type of data read from the file print('pic pic[80,90]',type(pic),type(pic[80,90]),pic[80,90]) #print('R R[80,90]',type(R),type(R[80,90]),R[80,90]) # turn into grayscale float np.array pic = pic0 = color.rgb2gray(pic) # this proves a conversion was done to float64 array print('pic pic[80,90]',type(pic),type(pic[80,90]),pic[80,90]) # t2 = time() - t0 print(' t1, t2 %7.4f %7.4f ' %(t1,t2)) # plt.figure(dpi=220) #(figsize=(6,6)) plt.imshow(pic, cmap='gray') plt.axis('off') plt.show() input(' blur? ') # # iterate Laplacian-based steps q = 1/5 N = pic.shape[0] M = pic.shape[1] print('N,M',N,M) # notice N