'getSubRectangles(ims)
Calculate the minimal rectangles that need updating each frame.
Returns a two-element tuple containing the cropped images and a
list of x-y positions.
Calculating the subrectangles takes extra time, obviously. However,
if the image sizes were reduced, the actual writing of the GIF
goes faster. In some cases applying this method produces a GIF faster.'

def getSubRectangles(self, ims):

    if (len(ims) < 2):
        return (ims, [(0, 0) for i in ims])
    if (np is None):
        raise RuntimeError('Need    Numpy    to    calculate    sub-rectangles.    ')
    ims2 = [ims[0]]
    xy = [(0, 0)]
    prev = ims[0]
    for im in ims[1:]:
        diff = np.abs((im - prev))
        if (diff.ndim == 3):
            diff = diff.sum(2)
        X = np.argwhere(diff.sum(0))
        Y = np.argwhere(diff.sum(1))
        if (X.size and Y.size):
            (x0, x1) = (X[0], (X[(-1)] + 1))
            (y0, y1) = (Y[0], (Y[(-1)] + 1))
        else:
            (x0, x1) = (0, 2)
            (y0, y1) = (0, 2)
        im2 = im[y0:y1, x0:x1]
        prev = im
        ims2.append(im2)
        xy.append((x0, y0))
    return (ims2, xy)
