Q:

Guassian image filtering plots with low stdev. values

I'm generating some basic Gaussian filtering demonstrations, but am getting peculiar output plots, in that they aren't linearly blurred. The below code imports any image and then applies a Gaussian filter of std (sigma) value 1, size 5.
I think it may be a finite vs infinite filter array assumption problem, but not sure. The conv2 function isn't my own but an analog of Matlabs conv2 function, I found on here.
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from scipy.ndimage.filters import convolve

def conv2(x,y):
    """
    This function the 2D convolution of two 2D input array x and y.

    Parameters
    ----------
    x : ndarray
    y : ndarray    

    Returns
    ----------
    z : convolved image output

    Usage:
    z = conv2(x,y,mode='same')

    """

    # Add singleton dimensions
    if (len(x.shape) < len(y.shape)):
        dim = x.shape
        for i in range(len(x.shape),len(y.shape)):
            dim = (1,) + dim
        x = x.reshape(dim)
    elif (len(y.shape) < len(x.shape)):
        dim = y.shape
        for i in range(len(y.shape),len(x.shape)):
            dim = (1,) + dim
        y = y.reshape(dim)

    origin = ()

    # Apparently, the origin must be set in a special way to reproduce
    # the results of scipy.signal.convolve and Matlab
    for i in range(len(x.shape)):
        if ( (x.shape[i] - y.shape[i]) % 2 == 0 and
             x.shape[i] > 1 and
             y.shape[i] > 1):
            origin = origin + (-1,)
        else:
            origin = origin + (0,)

    z = convolve(x,y, mode='constant', origin=origin)

    return z

# Question 1 - Guassian Blur
def Gaussian_filter(gamma, N):
    """
    Create the Gaussian filters
    """
    Amp = 1 / (2*np.pi*gamma**2)
    x = np.arange(-N,N,0.5)
    y = np.arange(-N,N,0.5)
    [x,y] = np.meshgrid(x,y)
    g = Amp * np.exp(-(x**2 + y **2)/(2*gamma**2))
    return g

im = Image.open("/home/will/Downloads/lenaTest3.jpg")
arr = np.array(im)

sigma=1
N=5
gaus = Gaussian_filter(sigma,5)

plt.figure(1)
plt.subplot(1,4,1)
plt.imshow(arr,cmap='gray')
plt.subplot(1,4,2)
plt.imshow(gaus,cmap='gray')
plt.subplot(1,4,3)
plt.imshow(conv2(arr,gaus),cmap='gray',vmin=0,vmax=255)
plt.subplot(1,4,4)  
plt.imshow(arr-conv2(arr,gaus),cmap='gray',vmin=0,vmax=255)

 
You can see on the right two images the Gaussian blur aren't regular. NOTE: it works fine for higher values of stdev (sigma) e.g. 10.

A:

Look like a problem of 8-bit integer overflow. You can work on float images, and avoid such problems: arr = np.array(im, dtype=np.float32).
BTW, why not use scipy.signal.convolve2d?
Much safer then converting some MATLAB code (I saw it gives different results from your function).
Your full code should look like this:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from scipy.signal import convolve2d

def Gaussian_filter(gamma, N):
    Amp = 1 / (2*np.pi*gamma**2)
    x = np.arange(-N,N,0.5)
    y = np.arange(-N,N,0.5)
    [x,y] = np.meshgrid(x,y)
    g = Amp * np.exp(-(x**2 + y **2)/(2*gamma**2))
    return g

if __name__ == "__main__":
    im = Image.open(r"C:\Users\eladj\Desktop\lena.jpg")
    arr = np.array(im, dtype=np.float32)

    sigma = 1
    N = 5

    gaus = Gaussian_filter(sigma,5)
    im_blurred = convolve2d(arr, gaus, mode='same')
    im_DoG = arr - im_blurred 

    plt.figure(1)
    plt.subplot(1,4,1)
    plt.imshow(arr, cmap='gray')
    plt.colorbar(fraction=0.046)
    plt.xticks([]), plt.yticks([])
    plt.subplot(1,4,2)
    plt.imshow(gaus, cmap='gray')
    plt.colorbar(fraction=0.046)
    plt.xticks([]), plt.yticks([])
    plt.subplot(1,4,3)
    plt.imshow(im_blurred, cmap='gray')
    plt.colorbar(fraction=0.046)
    plt.xticks([]), plt.yticks([])
    plt.subplot(1,4,4)  
    plt.imshow(im_DoG, cmap='gray')
    plt.colorbar(fraction=0.046)
    plt.xticks([]), plt.yticks([])

Notice the gray level values for each image.

